easy split of String()

Am I wrong or the arduino framework is missing an easy way to split a String() object? String() - Arduino Reference

I know that i can split char array with strtok and strtok_r but I would imagine an easier (less mind blowing) way to do it inside arduino

something like

String text = "this string have to be splitted";
String substring[10];

substring = text.split(" ");

and then substring[1] should contain "string"

the "split()" function could be based on "substring()" and "indexOf()"

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

mmmm I have heard this many times, and for this i always stayed away from them, but right now i am using a few of them in a project, mainly based on esp32 and 8266 and (since they have more memory) I didn't faced any error, yet. I tought about the possibility to use only cstring but String are not that bad for me now

in case someone cames around here looking for an easy split function i made one :slight_smile:

char *split(char str[], uint8_t index, char delimiter[] = " ")
{
	uint8_t counter = 0;
	char *token = strtok(str, delimiter);
	while (token != NULL)
	{
		if (counter == index)
		{
			return token;
		}
		token = strtok(NULL, delimiter);
		counter++;
	}
}