Go Down

Topic: Eliminate spaces, string.replace() and .trim() not possible? (Read 2832 times) previous topic - next topic

serial

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: [Select]

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

dxw00d

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: [Select]
/*
  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);
}

GoForSmoke

Just be aware that String class objects waste RAM which isn't exactly plentiful in Arduino.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts


Go Up