Pages: [1]   Go Down
Author Topic: Eliminate spaces, string.replace() and .trim() not possible?  (Read 2281 times)
0 Members and 1 Guest are viewing this topic.
NRW
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello good day Arduino communiy!

I allways got an error while compiling with the String- .trim() or .replace() function.

I`ve also tried some example code:
Code:
String stringOne = "<html><head><body>";
Serial.println(stringOne);
String stringTwo = stringOne.replace("<", "</");
Serial.println(stringTwo);


While compiling the sample source (Arduino v1.0) 8. Strings -> StringReplace:

Quote
StringReplace.cpp: In function 'void loop()':
StringReplace:22: error: conversion from 'void' to non-scalar type 'String' requested
StringReplace:28: error: conversion from 'void' to non-scalar type 'String' requested
StringReplace:29: error: no match for 'operator=' in 'leetString = leetString.String::replace('e', '3')'
C:\Users\...\Documents\Arduino\arduino-1.0\hardware\arduino\cores\arduino/WString.h:83: note: candidates are: String& String::operator=(const String&)
C:\Users\...\Documents\Arduino\arduino-1.0\hardware\arduino\cores\arduino/WString.h:84: note:                 String& String::operator=(const char*)

Thanks in advance
Best regards,

Micha
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the IDE release notes - http://arduino.cc/en/Main/ReleaseNotes

Quote
* The String class has been reimplemented as well, by Paul Stoffregen. This
  new version is more memory-efficient and robust. Some functions which
  previously returned new string instances (e.g. trim() and toUpperCase())
  have been changed to instead modify strings in place.

It would appear that the example sketches haven't been updated.

This version works:
Code:
/*
  String replace()
 
 Examples of how to replace characters or substrings of a string
 
 created 27 July 2010
 by Tom Igoe
 
 http://arduino.cc/en/Tutorial/StringReplace
 
 This example code is in the public domain.
 */

void setup() {
  Serial.begin(9600);
  Serial.println("\n\nString  replace:");
}

void loop() {
  String stringOne = "<html><head><body>";
  Serial.println(stringOne);
  // replace() changes all instances of one substring with another:
  stringOne.replace("<", "</");
  Serial.println(stringOne);

  // you can also use replace() on single characters:
  String normalString = "bookkeeper";
  Serial.println("normal: " + normalString);
  normalString.replace('o', '0');
  normalString.replace('e', '3');
  Serial.println("l33tspeak: " + normalString);

  // do nothing while true:
  while(true);
}
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4768
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just be aware that String class objects waste RAM which isn't exactly plentiful in Arduino.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

NRW
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

many thanks for the information!

works fine now  smiley
Logged

Pages: [1]   Go Up
Jump to: