Dear All,
Once again I turn to you all for aid. I created a few sketches some time ago that run pretty well. However, it's bloated with the String class. Mainly because the String class was (and still is) easy to use and low level C programming seemed too daunting for me. Now, as I grew older (a staggering 3,5 months) I come to realize that I may have to abandon the String class all together since I read up on Heap Memory Fragmentation. I have the following method for splitting Strings which works like a charm :
String getSplitValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
I can use it like this :
String toSplit = "Hello#World";
String segment = getSplitValue(toSplit , '#', 0);
The String variable Segment will then be Hello
Now I want to do the same with char
and I tried something like this :
char* subStr (char input_string, char *separator, int segment_number)
{
char *act, *sub, *ptr;
static char copy;
int i;
strcpy(copy, input_string);
for (i = 1, act = copy; i <= segment_number; i++, act = NULL)
{
sub = strtok_r(act, separator, &ptr);
if (sub == NULL) break;
}
return sub;
}
And it doesn't work :
char a = "Hello#World";
char _m = subStr(a, "#", 1);
I end up with a random character (always just one) which sometimes isn't even a part of the string at all. Can any of you point me in the right direction of what I am doing wrong ?
A little help would be highly appreciated. Thank you in advance.