white spaces

Hi, could anyone here help me how to get rid of the white spaces of the String(a) :confused: !!

String a = "GET /?led=21 HTTP/1.1"; void setup() { Serial.begin(9600); Serial.print(a); } void loop() {

}

Sure. Edit the string and remove the spaces. Well, the one space.

Better yet, also remove the String and replace it with a char string.

Muthana_alalaki: could anyone here help me how to get rid of the white spaces of the String(a) :confused: !!

I suspect you need to explain in more detail what you are trying to achieve.

...R

a = a.replace(" ", ""); :)

This sounds like an XY problem Most likely, the spaces are going to prove important when you try yo explain why you want to get rid of them. Surely you don’t intend to do something foolish like:

if(a == "GET/?led=21HTTP/1.1")
{

Do you?

Muthana_alalaki: could anyone here help me how to get rid of the white spaces of the String(a)

You also want to get rid of the text before and after the spaces, don't you?

First, I'd get rid of the "String" object!

"String" objects are bad.

Use C-strings (nullterminated strings, char arrays) only in your programs:

  char a[] = "GET /?led=21 HTTP/1.1";
  strtok(a," "); // advance to the first space token
  char* getrid=strtok(NULL," "); // advance to the second space token and store char pointer
  Serial.println(getrid); // print it

The only thing that is (a little bit) easier with "String" objects than with nullterminated strings is concatenating two String objects by using the plus sign '+' for doing so.

For all the rest of string programming you might want to do (except concatenating with '+') nullterminated strings are much more poverful and easier to use, when using some of the string functions defined in the AVR LIBC standard library.

The only thing that is (a little bit) easier with "String" objects than with nullterminated strings is concatenating two String objects by using the plus sign '+' for doing so.

Is:

String a = "Useless";
String b = "Crap";
String c = a + b;

REALLY easier than

char a[] = "Learn to"
char b[] = "do it right";
char c[50];
strcpy{c, a);
strcat(c, b);

?

PaulS: Is:

String a = "Useless";
String b = "Crap";
String c = a + b;

REALLY easier than

char a[] = "Learn to"
char b[] = "do it right";
char c[50];
strcpy{c, a);
strcat(c, b);

?

Yes, to a newcomer to C++

Newbie looks at your code and has to appreciate the use of char arrays, strcpy() and strcat() whereas the original code is obvious and requires practically no knowledge because it is almost written in English.

I fully understand the point that you are making but you are looking at it from your perspective which is vastly different than newcomers.

The String class is a problem because of the limited memory usage on the Arduino.

Muthana_alalaki: Hi, could anyone here help me how to get rid of the white spaces of the String(a) :confused: !!

Why do you want to?

UKHeliBob: I fully understand the point that you are making but you are looking at it from your perspective which is vastly different than newcomers.

With noobs, Arduino and text handling there are two main reasons that avoid leaning how to do things right:

  1. Somebody in the Arduino team decided to include "String" objects into the Arduino IDE distribution
  2. There are even example programs included in the Arduino IDE that are using "String" objects.

So thousands (if not millions) of noobs are led to a wrong way of dealing with text in their Arduino sketches, because somebody in the Arduino team decided to show the wrong way to all the Arduino noobs.

Shit happens.

And all of the noobs led on the wrong way to use "String" in their programs tend to have a question, when they want to do different text handling than concatenate two strings by using the plus sign. Then it gets really complicated...

  1. Somebody in the Arduino team decided to include "String" objects into the Arduino IDE distribution

Two wrongs don't make a right.

  1. There are even example programs included in the Arduino IDE that are using "String" objects.

And there a approximately 14 bazillion examples that do it right.

So thousands (if not millions) of noobs are led to a wrong way of dealing with text in their Arduino sketches

The same clods that can't understand the blink without delay example...

PaulS: And there a approximately 14 bazillion examples that do it right.

Arduino noobs almost never look at C++ tutorials and learn C/C++ programming with the standard C functions, neither from the Internet nor from a course in programming.

They just look at the programming examples instead.

And they find more than a dozen of "Strings" programming examples in the IDE at: "File" - "Examples" - "08. Strings"

They cannot find any example about typical C/C++ nullterminated strings in the Arduino-IDE.

That's why all the Arduino noobs think that "String" is what they should use for text handling in Arduino.

It's the Arduino-IDE itself whichs leads noobs the wrong way in text handling.

I think that all the "08" programming examples should be rewritten to deal with standard C functions and nullterminated strings, to perhaps: "File" - "Examples" - "08. text and strings"

I think that all the "08" programming examples should be rewritten to deal with standard C functions and nullterminated strings, to perhaps: "File" - "Examples" - "08. text and strings"

I whole-heartedly agree.