use*_*326 2 c c++ string arduino
I wrote the below in arduino code language (c or c++ ?) And i got puzled, i'm not sure if this is a limitation from C or C++. My function should split a text string and return word number x In my function i need to clean a string variable, that resets its contend until X is reached For readability X is called wordcount.
How do i clean the string Wordsample make it empty again ? On a side note, if the word isnt found then this function should also return nothing As the results are used to make other strings from.
String GetSubString (String A,int Wordcount) //for readability start counting B from 1
{ int CounterX;
String WordSampleN;
String result ;
for (int i = 0; i < A.length(); i++)
{ // split string
WordSampleN = WordSampleN + A[i];
if ((A[i] == ' ') || (A[i] =='\n'))
{ CounterX++;
if (CounterX == Wordcount)
{ result = WordSampleN;
}
if (CounterX <> WordCount)
{ WordSampleN = ''; // <== ERROR IS HERE
}
}
}
return result;}
Run Code Online (Sandbox Code Playgroud)
On a side note, if possible I would like to keep use strings of any size, not fixed strings.
没有“空字符”的概念。您不能以这种方式初始化您的字符串(对象)。相反,使用一个空的(小-“s”)字符串“”;
WordSampleN = "";
Run Code Online (Sandbox Code Playgroud)
这将调用采用 c 样式字符串的 String 构造函数,并使用空字符串初始化 String 对象。