Split char by delimiter and index

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.

  static char copy;
...
  strcpy(copy, input_string);

didn't you get a compiler warning? (you allocated memory for 1 character and want to copy a full string in it)

Look at the functions available to you in the standard cstring C library

The parse example in Serial Input Basics illustrates the use of delimiters to split cstrings

...R

I did read this, but unfortunately it didn't provide me the answer I was looking for although it came very close. Nice article and it certainly helped me in my quest.

have you noticed my post #1? you have a nasty bug to address

@J_M_L,

No, I did not get a compiler warning, not even a moan. However, that was in the end exactly where the problem was, thank you for pointing that out to me. I fix it by changing the function to this :

char* subStr (char* input_string, char *separator, int segment_number) 
{
  char *act, *sub, *ptr;
  static char copy[100];
  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 then call it like this :

char *_m = subStr("Hello#World", "#", 1);

Thank you once again.

Yep - you impact your memory heavily without a way to release that memory though and you can't call the function twice with different data without loosing the info from the first call.

Usually when you design such function you return the pointer inside the buffer that was passed to you upon calling. (of course challenging if you use strtok because it damages that string)

True, I agree .. but I welcome additions/improvements to this method of course. I mean, if there's a better way to achieve this then I'm all ears.

as I said in #1 Look at the functions available to you in the standard cstring C library

for example strstr() is not destructive

Hactic:
I did read this, but unfortunately it didn't provide me the answer I was looking for although it came very close.

If you explain where it falls short I may be able to help.

...R