I am trying to split a System::String at ":" using System::String::Split in my C++ code. The string to be split is called "responseString". It is a System::String. I have:
char id[] = { ':' };
return responseString->Split(id);
However, it errors at the "->" saying that no instance of the overloaded function matches the argument list. I checked the MSDN documentation, and see no information on doing this in C++.
I also tried the following, with the same results:
System::Char id[] = { ':' };
return responseString->Split(id);
In the documentation it shows the following example, but I do not know what to do with that:
array<String^>^ Split(
... array<wchar_t>^ separator
)
Any help would be appreciated!
Symbol delimiter can be either of type wchar_t, either String ^, as well as their array.
Here's a quick example:
String ^str="String:need:split;this";
array<wchar_t> ^id = { ':' ,';'};
array<String^> ^StringArray =str->Split(':');
array<String^> ^StringArray2 =str->Split(id);
for each(String^ temp in StringArray)
Console::WriteLine(temp);
Console::WriteLine();
for each(String^ temp in StringArray2)
Console::WriteLine(temp);
Related
I'm trying to concatenate some strings which will eventually be part of a URL, but the result of the concatenation is always missing the last String which is a file extension.
I've tried all the official ways of concatenating. Here is my latest attempt when I tried to merge the strings by using the join method on a String List.
String? resColor = color?.label;
String? resCategory = category?.label;
print(resColor!.length);
List<String> refSplit = [
'previewAssets/',
resCategory!,
'/',
resColor!,
'.jpg'
];
for (int i = 0; i < refSplit.length; i++) {
print(refSplit[i]);
}
String ref = refSplit.join('');
print(refSplit);
print(ref);
My excepted outcome is obviously that ref would contain all items from refSplit. But it doesn't.
Here is the output from the prints: output
Im knida new to flutter but I feel like concatenating strings shouldn't be this hard, so I'm probably missing something obvious. Any help would be greatly appreciated.
*edit: So I've continued to look into this and I'm pretty sure the problem is with resColor, which suspiciously has a length of 4 when it should be 3. So my current guess is that it contains some invisible endline like char at the end which is messing up the concat.
**edit: By removing the last char of resColor, it fixed the issue. This is the code I used to do that: resColor = resColor!.substring( 0, resColor!.length -1);
I tried your code and it seems to work fine in dartpad
void main() {
List<String> refSplit = [
'previewAssets/',
'something',
'/',
'109',
'.jpg'
];
String ref = refSplit.join("");
print(ref);
}
Try to restart your IDE and try again.
I am looking for a way to get a String between 2 Strings using Arduino. This is the source String:
Hello, my name is John Doe# and my favourite number is 32#.
The output has to be:
String name = "John Doe"; //Between "name is " and "#"
String favouriteNumber = "32"; //Between "number is " and "#"
How can this be achieved with Arduino?
I am not able to find any information online about this. Those examples for C are not working anyway. I understand that using String is not recommended in Arduino, but I have to do it this way to make things simpler.
By the way, this method of using a '#' to indicate the end of the data is not an ideal way to do it as I would like the input to be more human readable and more natural. Would anyone please suggest another way to do this as well?
Thanks in advance!
Function midString find the substring that is between two other strings "start" and "finish". If such a string does not exist, it returns "". A test code is included too.
void setup() {
test();
}
void loop() {
delay(100);
}
String midString(String str, String start, String finish){
int locStart = str.indexOf(start);
if (locStart==-1) return "";
locStart += start.length();
int locFinish = str.indexOf(finish, locStart);
if (locFinish==-1) return "";
return str.substring(locStart, locFinish);
}
void test(){
Serial.begin(115200);
String str = "Get a substring of a String. The starting index is inclusive (the corresponding character is included in the substring), but the optional ending index is exclusive";
Serial.print(">");
Serial.print( midString( str, "substring", "String" ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "substring", "." ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "corresponding", "inclusive" ) );
Serial.println("<");
Serial.print(">");
Serial.print( midString( str, "object", "inclusive" ) );
Serial.println("<");
}
just searched for this and saw no answer so i cooked one up.
i prefer working with String as well because of code readability and simplicity.
for me its more important than squeezing every last drop of juice out of my arduino.
String name = GetStringBetweenStrings("Hello, my name is John Doe# and my favourite number is 32#." ,"name is ","#");
String GetStringBetweenStrings(String input, String firstdel, String enddel){
int posfrom = input.indexOf(firstdel) + firstdel.length();
int posto = input.indexOf(enddel);
return input.substring(posfrom, posto);
}
watch out for the first case its fine, but for the second one you would have to change the second filter sting to "#." so it doesn't use the first occurrence of the #
string str1=tkr;
for(i=1;i<=10;i++)
{
string str2=str1+i;
sopln(str2);
}
I need result like this..
tkr1
tkr2
tkr3
tkr4
tkr5...could any one re-write the code which could give the proper output?
Its not mentioned which language you are using so it would be not possible to give exact answer:
This should work in most of the languages :
String str1=tkr;
String str2 = "";
for(i=1;i<=10;i++)
{
str2=str2 + str1+i + " ";
}
print(str2)
And use mutable strings if you are using java
I have an openFileDialog that returns a filename. I want to save that filename as a char* so that I can open it later using fstream. To do this I'll need to replace all instances of \ with \\ in the string.
My tactic for doing this is to split the System::String^ on every instance of \, and then join all the elements of the resulting System::String^ array back together with \\ separating them.
This is the function that I have written, but it seems to return a blank string even when I pass a String^ that contains \.
private: const char* getCharPointer(String^ name){
array<String^>^ words;
String^ delimStr = "\\";
array<Char>^ delimiter = delimStr->ToCharArray( );
String^ replaceDelim = "\\\\";
words = name->Split(delimiter);
String^ tidiedName = String::Join( replaceDelim, words );
label1->Text = tidiedName;
std::string newname=msclr::interop::marshal_as< std::string >( tidiedName);
const char* name_cstr = newname.c_str();
return name_cstr;
}
I am a newcomer to Visual C++ and Windows in general so I would appreciate any pointers on this. It doesn't help that IntelliSense doesn't seem to work with Visual Studio 2010.
Intellisense is not provided in VC++ and It seems it will never be added into VC++.
Anyway, you can use, instead of Join, Replace.
You can see here the reference.
Look at this simple program [ I'm using a Windows Form Application and this code is under a button event handler].
String^ a = "Hello\\World";
String^ b = a->Replace("\\", "\\\\"); //Replace \ with \\
MessageBox::Show(a + " -> " + b);
if you run it, you will get this output.
Hello\World -> Hello\\World
I hope this one is what you need for your code!
I'm wondering how (and in which way it's best to do it) to split a string with a unknown number of spaces as separator in C++/CLI?
Edit: The problem is that the space number is unknown, so when I try to use the split method like this:
String^ line;
StreamReader^ SCR = gcnew StreamReader("input.txt");
while ((line = SCR->ReadLine()) != nullptr && line != nullptr)
{
if (line->IndexOf(' ') != -1)
for each (String^ SCS in line->Split(nullptr, 2))
{
//Load the lines...
}
}
And this is a example how Input.txt look:
ThisISSomeTxt<space><space><space><tab>PartNumberTwo<space>PartNumber3
When I then try to run the program the first line that is loaded is "ThisISSomeTxt" the second line that is loaded is "" (nothing), the third line that is loaded is also "" (nothing), the fourth line is also "" nothing, the fifth line that is loaded is " PartNumberTwo" and the sixth line is PartNumber3.
I only want ThisISSomeTxt and PartNumberTwo to be loaded :? How can I do this?
Why not just using System::String::Split(..)?
The following code example taken from http://msdn.microsoft.com/en-us/library/b873y76a(v=vs.80).aspx#Y0 , demonstrates how you can tokenize a string with the Split method.
using namespace System;
using namespace System::Collections;
int main()
{
String^ words = "this is a list of words, with: a bit of punctuation.";
array<Char>^chars = {' ',',','->',':'};
array<String^>^split = words->Split( chars );
IEnumerator^ myEnum = split->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ s = safe_cast<String^>(myEnum->Current);
if ( !s->Trim()->Equals( "" ) )
Console::WriteLine( s );
}
}
I think you can do what you need to do with the String.Split method.
First, I think you're expecting the 'count' parameter to work differently: You're passing in 2, and expecting the first and second results to be returned, and the third result to be thrown out. What it actually return is the first result, and the second & third results concatenated into one string. If all you want is ThisISSomeTxt and PartNumberTwo, you'll want to manually throw away results after the first 2.
As far as I can tell, you don't want any whitespace included in your return strings. If that's the case, I think this is what you want:
String^ line = "ThisISSomeTxt \tPartNumberTwo PartNumber3";
array<String^>^ split = line->Split((array<String^>^)nullptr, StringSplitOptions::RemoveEmptyEntries);
for(int i = 0; i < split->Length && i < 2; i++)
{
Debug::WriteLine("{0}: '{1}'", i, split[i]);
}
Results:
0: 'ThisISSomeTxt'
1: 'PartNumberTwo'