Go Down

Topic: split() (Read 729 times) previous topic - next topic

cbrandy

Hi guys.
I'm trying desperately to figure this out.
In Visual Basic I use split() function. In C there is none!

Or is it?

VB.net code
Code: [Select]


        Dim txtData As String = SerialPort1.ReadLine

        RichTextBox1.Text = txtData

        Dim myFile As String = txtData
        myFile = Replace(myFile, " ", "")

        Dim i As Integer
        Dim aryTextFile() As String
        aryTextFile = myFile.Split(",")

        'Sett inn sensorene her
        For i = 0 To UBound(aryTextFile)
            MsgBox(i)
        Next i



How could I do this in C?

Best,
Andy

ChrisS

Hi!

MMh, you want to read Serial and get its values into an array... it seems so to me.
So you have to declare an array, and then simply put the values into it.
ahm, seems that you want to get rid of " "....

First Step I think...
Code: [Select]


char myvalues[count of Values];   // Declare a Array with X cells.
int counter=0;                           // Declare a counter which holds ints

void setup(){
Serial.beginn(19200);                  //Serial.open with 19200 Speed.
}

void loop(){
myvalues[counter]=Serial.read();   //Read a Value and put it into the Array.
counter++;                                 // Increase Counter to get to the next Arraycell.
delay(50);                                  // This slows down, to avoid a bufferoverrun.
}



Thats maybe buggy (not tested),
AND it's NOT the best way....

but with this in scheme you should get
the serial incomming Values into a onedimensional Array.

Hope, this is a startingpoint for you.

Greetings ChrisS
Sturmfabrik - mediale Dienstleistungen
www.sturmfabrik.de

ChrisS

Sure but not every Microcontroller have 1K...

Stringmanipulation has been introduced with 021 022 officially.

I am using a 1284p with 16KB Sram and 128KB flash for example.

Simple manipulation is possible i think.

Greetings ChrisS
Sturmfabrik - mediale Dienstleistungen
www.sturmfabrik.de

PaulS

The strtok function can be used, in a while loop, to extract tokens that are delimited by any character(s).

Code: [Select]
char test[] = "This is a string with many words";
char *token = strtok(test, " ");
// token will be "This"
while(token)
{
   // Do something with this word

   token =  strtok(NULL, " "); // Use NULL as the 1st argument to keep parsing the same string
   // token will be is, a, string, etc. in order
}

Go Up