system
August 19, 2014, 4:19pm
1
I am not getting the String length using the following code.
What am I missing?
Any constructive help will be appreciated.
String txtMsg = "TEST STRING "; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String
if(lastStringLength)
{
lcd.setCursor(0,1);
lcd.print(lastStringLength);
Serial.println(lastStringLength);
}
else
Serial.println (" Empty string ");
system
August 19, 2014, 4:40pm
2
What am I missing?
setup(), loop(), and proof.
Constructive help:
Ditch String.
Use string.h and strlen().
More constructive help:
Provide a compilable, runnable example.
This code:
void setup(void)
{
Serial.begin(9600);
while(!Serial);
delay(2000);
String txtMsg = "TEST STRING "; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String
if(lastStringLength)
{
Serial.print("String length is ");
Serial.println(lastStringLength);
} else {
Serial.println (" Empty string ");
}
}
void loop(void)
{
}
prints
String length is 12
You're doing something wrong which you haven't shown us.
Pete
system
August 19, 2014, 4:52pm
5
Any constructive help will be appreciated.
So would you posting complete code, so we can reproduce the problem, or proving some proof that the String length is 0.
String crap = "This is not a short String";
void setup()
{
Serial.begin(115200);
Serial.print("String: {");
Serial.print(crap);
Serial.println("]");
Serial.print("length: " );
Serial.println(crap.length());
}
void loop()
{
}
String: {This is not a short String]
length: 26
Is your length() method in the global area? It might need to be in an actual code section.
system
August 19, 2014, 5:03pm
7
PaulS:
What am I missing?
setup(), loop(), and proof.
What he said.
+1.
Great generic answer here
system
August 19, 2014, 6:17pm
8
KeithRB:
Constructive help:
Ditch String.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
String txtMsg = "TEST String";
int lastStringLength = txtMsg.length();
void setup ()
{
Serial.begin (115200);
lcd.begin(16,2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
if(lastStringLength)
{
lcd.print(lastStringLength);
Serial.println(lastStringLength);
lcd.setCursor(0,1);
lcd.print(txtMsg);
}
else
{
Serial.println ("Empty string");
lcd.print ("Empty string");
}
}
void loop ()
{}
7714 bytes.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
char* txtMsg = "TEXT string";
int lastStringLength = strlen (txtMsg);
void setup ()
{
Serial.begin (115200);
lcd.begin(16,2);
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
if(lastStringLength)
{
lcd.print(lastStringLength);
Serial.println(lastStringLength);
lcd.setCursor(0,1);
lcd.print(txtMsg);
}
else
{
Serial.println ("Empty string");
lcd.print ("Empty string");
}
}
void loop ()
{}
6118 bytes
Ditch String.
Constructive advice { nods assertively }
Vaclav:
I am not getting the String length using the following code.
What am I missing?
Any constructive help will be appreciated.
Turning that into a sketch ...
void setup ()
{
Serial.begin (115200);
String txtMsg = "TEST STRING "; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String
if(lastStringLength)
{
Serial.println(lastStringLength);
}
else
Serial.println (" Empty string ");
}
void loop () { }
I get the output:
12
So it doesn't "always" return 0.
String txtMsg = "TEST STRING "; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String
Also, since you are assigning when you create the variable lastStringLength it will not be the "previous" length of the string, it will be the first length.
system
August 20, 2014, 3:49am
11
Thanks everybody.
I have something wrong elsewhere in MY code.
system
August 20, 2014, 6:50am
12
Vaclav:
I have something wrong elsewhere in MY code.
Who'd've guessed?
That's five minutes of my life I'm not getting back.