Pages: [1]   Go Down
Author Topic: replace()  (Read 551 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
I have this project where I use split() and replace().
Now I have a bad feeling that I can't use char* split() and replace() togehter.

code:
Code:
#include <string.h>

void setup() {
  Serial.begin(19200);
}

void loop(){
char test[] = "s1: 22, s2: 43, s3: 12, s4: 56";
test = test.replace(' ', '');
char *token = strtok(test, ",");
int i = 1;

while(token)
{  // Do something with this word
Serial.print(token);
Serial.println();
token = strtok(NULL, ",");
i +=1;
}
Serial.println();
delay(1000);
}

Cause I get this error message:
split:8: error: request for member 'replace' in 'test', which is of non-class type 'char [31]'

Anny way to solve this?

-Andy
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can not call methods on variables of scalar datatypes. You need to look for some c-style function for replacing chars in a c-string.
If you're up for the challenge, you could write your own replace function smiley
Logged

Germany
Offline Offline
Edison Member
*
Karma: 136
Posts: 1491
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps, the use of
  char *token = strtok(test, " ,");
and
  token = strtok(NULL, " ,");
(note: " ," instead of ",") might solve your problem
without removing the blanks (if this is intended by replace(' ', '')).

Oliver
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Test is an array of char, not a class with member functions, therefor the replace() is not existant => compiler error
From the code I see you want to remove the spaces, you can use an inplace remove like below

Code:
#include <string.h>

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  char test[] = "s1: 22, s2: 43, s3: 12, s4: 56";
  // test = test.replace(' ', '');
    Serial.println(test);
  remove(test, ' ');
  Serial.println(test);
  
  char *token = strtok(test, ",");
  int i = 1;

  while(token)
  {  // Do something with this word
    Serial.print(token);
    Serial.println();
    token = strtok(NULL, ",");
    i +=1;
  }
  Serial.println();
  delay(1000);
}


void remove(char *str, char r)
{
  char *p, *q;
  p = q = str;
  while (*p != '\0')
  {
    while (*p == r) p++;
    *q++ = *p++;
  }
  *q = '\0';
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Norway
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sweet robtillaart.
Thanks for your help smiley
It works!

Now I'm just implent in my own code...

-Andy
Logged

Pages: [1]   Go Up
Jump to: