# Problem splitting String in Arduino.

**URL:** <https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171>\
**Category:** Programming\
**Created:** [February 28, 2012, 9:13am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171 "2012-02-28T09:13:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 28, 2012, 9:13am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/1 "2012-02-28T09:13:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 28, 2012, 11:13am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/2 "2012-02-28T11:13:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SPlatten](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/splatten/32/217947_2.png) [@SPlatten](https://forum.arduino.cc/u/SPlatten)\
**Post date:** [February 28, 2012, 12:48pm UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/3 "2012-02-28T12:48:22Z")

</div>

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:

```cpp
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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 28, 2012, 1:47pm UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/4 "2012-02-28T13:47:55Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![SPlatten](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/splatten/32/217947_2.png) [@SPlatten](https://forum.arduino.cc/u/SPlatten)\
**Post date:** [February 28, 2012, 3:58pm UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/5 "2012-02-28T15:58:10Z")

</div>

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:

```cpp
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++.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 29, 2012, 8:43am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/6 "2012-02-29T08:43:41Z")

</div>

still not working.☹

#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]);

}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 29, 2012, 8:49am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/7 "2012-02-29T08:49:30Z")

</div>

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

[http://www.cplusplus.com/reference/clibrary/cstring/strtok/](http://www.cplusplus.com/reference/clibrary/cstring/strtok/)

Do you understand the difference between **S** tring objects and C type **s** trings? If not, some research in that area might be worthwhile.

---

<div class="post-metadata">

**Author:** ![SPlatten](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/splatten/32/217947_2.png) [@SPlatten](https://forum.arduino.cc/u/SPlatten)\
**Post date:** [February 29, 2012, 10:15am UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/8 "2012-02-29T10:15:21Z")

</div>

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:

```cpp
	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;
	}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:44pm UTC](https://forum.arduino.cc/t/problem-splitting-string-in-arduino/92171/9 "2021-05-05T19:44:12Z")

</div>


