Go Down

Topic: Substr sample (Read 3 times) previous topic - next topic

Bontempos

This is a very very basic sample using Substr in Arduino.
For those migrating from as3, hope this might be useful just to get some of the idea.
This code will only take a string containing time information and split it using substr instead of tokenizing it.

Code: [Select]


#include <WString.h>

void setup(){
 Serial.begin(9600);
 printTime("200912300840107");
}

void loop(){
}

void printTime(String sysTime){
 
 String year,month,date,hour,minute,seconds,day; //declaring all vars as String Type.

 year   = sysTime.substring(0,4);
 month  = sysTime.substring(4,6);
 date   = sysTime.substring(6,8);
 hour   = sysTime.substring(8,10);
 minute = sysTime.substring(10,12);
 seconds= sysTime.substring(12,14);
 day    = sysTime.substring(14,15);

 Serial.println(year);
 Serial.println(month);
 Serial.println(date);
 Serial.println(hour);
 Serial.println(minute);
 Serial.println(seconds);
 Serial.println(day);
 
}


advanced C++ programers, please upgrade the code if you've got better ways to achive the same results ;)

Btp~

Coding Badly

#1
Dec 20, 2009, 08:34 pm Last Edit: Dec 20, 2009, 08:34 pm by bcook Reason: 1
Quote
advanced C++ programers, please upgrade the code if you've got better ways to achive the same results

You'll need to describe what you mean by "better"?  Less code?  Faster?  Easier to read / understand?  Easier to debug?

gport

Thanks for the code sample.

Unfortunately it just 'hangs' on my Arduino (0018) but was just what I required as I thought I was going mad not being able to get the 'substring' function to work in my sketch...so this has confirmed it.

Strange, as all the other WString functions work as advertised...just not this one...and typical it is the one I really want!!!!

Does any one else have any 'substr' functions out there?

Many thanks,

GS


gport

Have got round it I think using some of the other functions (@ and # temporary delimiters) :

Code: [Select]

int first = inString.indexOf('@');
int second = inString.indexOf('#');

for (int i = (first + 1); i <= (second - 1); i++){
     Serial.print(inString.charAt(i));



seems to work OK  :)

Go Up