A Function for print a long String on LCD

I wanted to create a function that I could use whenever I wanted to print a String on LCD 16x2 i2c.
But when I add numbers to String the function breaks, And got stuck.

This is the function
The String user is a String that I use in void loop and and changes it to whenever I want to print.

void lcdPrint()
{
  int leng= user.length(), index;
  String lcdM;
  if (leng>16)
  {
   leng=0;
   leng= user.indexOf(" ", leng) +1;
   while(leng<=16)
   {
    leng= user.indexOf(" ", leng) +1;
    if (leng<+16){index=leng;}
   }
   lcdM= user.substring(0,index);
   lcd.setCursor(0,0);
   lcd.print(lcdM);
   lcdM= user.substring(index);
   lcd.setCursor(0,1);
   lcd.print(lcdM);
  }
  else
  {
    lcd.setCursor(0,0);
    lcd.print(user);
  }
}

Please start by explaining what the purpose of the function is, how it would typically be used and what goes wrong when you add numbers to the String

Usually I want to print a string like "hello world how are you today?"
I use like this

user= "hello world how are you today?";
lcd.clear();
lcdPrint();

But when I try to add a number like that

int books=32;
user="there is "+books+"In this room";
lcd.clear();
lcdPrint();

The screen is blank, And shows nothing.
Hope I explained well

You are trying to concatenate an int with a String and whilst I know very little about Strings because I generally don't use them, such a concatenation is unlikely to work

Does user="there is "+books+"In this room"; even compile ?

What if you cast books to String?

One of your lines won't compile for me. Please show a small sketch that compiles with those lines.

void setup()
{
  Serial.begin(115200);
  String user;

  user = "hello world how are you today?";
  Serial.println(user);

  int books = 32;
  user = "there is " + books + "In this room";
  Serial.println(user);
}
void loop() {}
Arduino: 1.8.13 (Mac OS X), Board: "Arduino Uno"
/Users/john/Documents/Arduino/sketch_may19a/sketch_may19a.ino: In function 'void setup()':
sketch_may19a:10:30: error: invalid operands of types 'const char*' and 'const char [13]' to binary 'operator+'
   user = "there is " + books + "In this room";
          ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
exit status 1
invalid operands of types 'const char*' and 'const char [13]' to binary 'operator+'

Changing the line to
user = "there is " + (String)books + "In this room";
results in this output:

hello world how are you today?
there is 32In this room

yes, either call String() to convert the number into a String you can concatenate or do it in chunks

String message;
int books = 32;

void setup()
{
  Serial.begin(115200);
  message.reserve(50); // limit heap fragmentation risk (in general, here it's not really needed)
  message = "there are " + String(books) + " books in this room";
  Serial.println(message);

  message = "there are ";
  message += books; // this knows how to deal with concatenation with an integer
  message += " books in this room";
  Serial.println(message);
}
void loop() {}

I'd be careful about using String

you could also just print things separately and you'll save memory...

int books = 32;
void setup()
{
  Serial.begin(115200);
  Serial.print(F("there are "));
  Serial.print(books);
  Serial.println(F(" books in this room"));
}
void loop() {}

You're right it's not compile, but if I write it

user = "there is "
user=user+ books;
user=user+" in the room"

this compile

Thanks it works now.
But if I put a space after the number so

user = "there is " + (String)books + " In this room";

the screen will be blank but the code keeps running.

Does your code allow for the fact that there is more than one space in the String ?

I used chunks, But the problem was that after I create the String I do not get the result I want on the LCD.
I want to use String for a few more things this is why I use it.

Yes he is supposed to count the spaces.
This is how I count the amount of words, So that I will not cut the words.

Have you explained what your function actually does (or should do) ?

In the big picture print on LCD 16x2 bigger Strings.
in details:

  • I check if the String is larger than 16 characters
  • Then I check where there is space Then add one and check where the next space is, Checks the length and if it is shorter than 16 characters I continue
  • When it is longer than 16 characters I cut the String and print