Problem splitting String in Arduino.

Hi I am trying to split a String "-7,10,4" in Arduino Code

String myString = "-7,10,4";
int sensors[] = int(split(myString, ','));
Serial.println(sensors[0]);
Serial.println(sensors[1]);
Serial.println(sensors[2]);

Anyone please tell me how to write it for Arduino.

Why is this a poll? Where's the "I'm a croosposting doofus" option?

Surely split should return and array of char*[], where each pointer points to the string after it has been split, in your example where you are splitting by the delimiter ',', the return would be:

char* pResults[] = split( "-7,10,4", ',' );

pResults[0] would point to -7
pResults[1] would point to 10
pResults[2] would point to 4

Thats how split normally works, explode in php works the same way.

Actually I am getting the values in a variable named pageValue, which is datatype is String.

String pageValue ;

So I can't use char* pResults[] = split( pageValue, ',' );
char & String is conflicting together error :frowning:

You could create your own class which is derived from String, then in that class define split as a public method.

Your pageValue could be of the new class type and then you could call it:

String aryStrings[] = pageValue.split( "," );

I have an implementation of split which I wrote in Java if you are interested, could easily be adapted to C++.

still not working.:frowning:

#include <string.h>
String pageValue = "a1,b1,c1,d1,e1,f1";

void setup(){

// String aryStrings[] = pageValue.split( "," ); // Java script code
String aryStrings[] = strtok( pageValue, "," ); // C++ code
Serial.begin(9600);
}

void loop()
{
Serial.println(aryStrings[0]);
Serial.println(aryStrings[1]);
Serial.println(aryStrings[2]);
Serial.println(aryStrings[3]);
Serial.println(aryStrings[4]);
Serial.println(aryStrings[5]);

}

Not surprised that it doesn't work. You need to read up on strtok(). It isn't a String function. It works on C type strings, meaning NULL-terminated char arrays.

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Do you understand the difference between String objects and C type strings? If not, some research in that area might be worthwhile.

Here is my java code for split....the only reason I wrote this was because the JVM I'm using on a particular platform is old and doesn't have the split routine:

	public static String[] splitBuffer( char[] aryBuffer, String strDelimiter ) {
		try{
			if ( aryBuffer == null || aryBuffer.length == 0) {
				throw new Exception( "Invalid buffer!" );
			}
			if ( strDelimiter == null || strDelimiter.length() == 0 ) {
				throw new Exception( "Invalid delimiter!" );
			}
// Count the instances of the delimiter
			String[] aryRecords = null;			
			for( int p=0; p<2; p++ ) {
				int intLines = 0; 
				for( int b=0; b<aryBuffer.length; b++ ) {
// Look for the delimiter which could be multiple bytes
					int d, t=b;					
					for( d=0; d<strDelimiter.length(); d++, t++ ) {
						if ( aryBuffer[t] != strDelimiter.charAt(d) ) {
							break;							
						}
					}
					if ( p == 1 && d != strDelimiter.length() ) {
						if ( aryRecords[intLines] == null ) {
							aryRecords[intLines] = "";
						}
						aryRecords[intLines] += aryBuffer[b]; 
					} else if ( d == strDelimiter.length() ) {
// Re-initialise the start reference byte, skipping over the delimiter
						b += strDelimiter.length();
// Subtract 1 as the loop with add 1						
						b--;
// Increment the total number of lines						
						intLines++;
					}
				}
				if ( p == 0 && intLines > 0 ) {
// Create the string array				
					aryRecords = new String[ (intLines + 1) ];
				}					
			}
			return aryRecords;
		} catch( Exception ex ) {
// Report exception here...
		}
		return null;
	}