Pages: [1]   Go Down
Author Topic: Substr sample  (Read 2907 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 57
[ch12420][ch12387][ch12401][ch12426]!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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 smiley-wink

Btp~
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 199
Posts: 12768
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: December 20, 2009, 02:34:31 pm by bcook » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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  smiley
Logged

Pages: [1]   Go Up
Jump to: