# How to create efficent strings

**URL:** <https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403>\
**Category:** Programming\
**Created:** [August 11, 2026, 9:57am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403 "2026-08-11T09:57:42Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![grantkbrown](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grantkbrown/32/1276968_2.png) [@grantkbrown](https://forum.arduino.cc/u/grantkbrown)\
**Post date:** [August 11, 2026, 9:57am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/1 "2026-08-11T09:57:42Z")

</div>

Hi All,

I'm looking for those that know about strings in Arduino better than me to give me some guidance

The purpose of my question is to to guide me to the most efficient way to create a string from multiple variables in order to update a 4 line LCD display using LiquidCrystal\_I2C.h

I'm currently using (uint8\_t variables) but I need to allow for int's and reals'

But I suspect my approach is wrong in terms of program storage space and dynamic memory

```arduino
        LCD_4_Line.clear();
        LCD_4_Line.setCursor(0, 0);
        DisplayString = DisplayString + "/";
        DisplayString = DisplayString + String(Month);
        DisplayString = DisplayString + "/";
        DisplayString = DisplayString + String(Year);
        DisplayString = DisplayString + " ";
        DisplayString = DisplayString + String(Hour);
        DisplayString = DisplayString + ":";
        DisplayString = DisplayString + String(Minute);
        DisplayString = DisplayString + "pm";
      LCD_4_Line.print(DisplayString);

```

Can anyone guide me to a better approach to this

As always any guidance given is very much appricated

Grant Brown

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [August 11, 2026, 10:05am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/2 "2026-08-11T10:05:22Z")

</div>

> [@grantkbrown](#):
>
> from multiple variables in order to update a 4 line LCD display using LiquidCrystal\_I2C.h

Give an example of the data/message that you want to show on the 4x20 LCD.

11/8/2026 4:07:37 PM

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 11, 2026, 10:06am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/3 "2026-08-11T10:06:48Z")

</div>

> [@grantkbrown](#):
>
> e most efficient way to create a string from multiple variables in order to update a 4 line LCD display using LiquidCrystal\_I2C.h

why do you need to create the String in the first place ?

You could (and should possibly) be updating each field separately when needed - no need to repaint the whole lines / screen

or just print it, like

```arduino
LCD_4_Line.clear();
LCD_4_Line.setCursor(0, 0);
LCD_4_Line.print("/");
LCD_4_Line.print(Month);
LCD_4_Line.print("/");
LCD_4_Line.print(Year);
LCD_4_Line.print(" ");
LCD_4_Line.print(Hour);
LCD_4_Line.print(":");
LCD_4_Line.print(Minute);
LCD_4_Line.print("pm");

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 11, 2026, 10:13am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/4 "2026-08-11T10:13:21Z")

</div>

Short answer

Don't use Strings

Use arrays of chars and format them using the snprintf() function.

```cpp
int Month = 12;
int Year = 2026;
int Hour = 8;
int Minute = 14;

char buffer[30];

void setup()
{
    Serial.begin(115200);
    snprintf(buffer, sizeof(buffer), "/%d/%d %d:%dpm", Month, Year, Hour, Minute);
    Serial.println(buffer);
}

void loop()
{
}

```

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [August 11, 2026, 10:19am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/5 "2026-08-11T10:19:41Z")

</div>

If it's just once, `print` each part separately. But generally

> [@grantkbrown](#):
>
> in terms of program storage space and dynamic memory

`snprintf` can avoid dynamic memory entirely.

If you still want to use `String`, first `reserve` the amount of space you expect to use, which attempts a single allocation. Then use `concat`, which has overloads for the various types (much like `print`)

```cpp
String display;
display.reserve(30);
display.concat('/');
display.concat(month);
// etc

```

Both methods return a `bool` to indicate whether they actually worked or ran out of memory -- not that there's much you can do if it fails.

---

<div class="post-metadata">

**Author:** ![grantkbrown](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grantkbrown/32/1276968_2.png) [@grantkbrown](https://forum.arduino.cc/u/grantkbrown)\
**Post date:** [August 11, 2026, 10:26am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/6 "2026-08-11T10:26:39Z")

</div>

> [@UKHeliBob](#):
>
> `"/%d/%d %d:%dpm"`

Is the some explanations as-to "/%d/%d %d:%dpm"

Grant

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [August 11, 2026, 10:32am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/7 "2026-08-11T10:32:38Z")

</div>

The `%` are _format specifiers_. [This](https://cplusplus.com/reference/cstdio/printf/) doc page is more approachable than [this](https://en.cppreference.com/cpp/utility/format/spec) one. One advantage over `String` is being able to specify leading zeroes with widths; e.g. `%02d/%02d/%02d` for dates.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 11, 2026, 10:33am UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/8 "2026-08-11T10:33:25Z")

</div>

That is the formatting string and there is plenty of online help such as [https://cplusplus.com/reference/cstdio/printf/](https://cplusplus.com/reference/cstdio/printf/)

In the case of my example the %d parameters are placeholders for integer data items and all the other characters are concatenated with them. The data to be concatenated is in the comma separated list at the end of the snprintf() function

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [August 11, 2026, 12:22pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/9 "2026-08-11T12:22:29Z")

</div>

> [@grantkbrown](#):
>
> The purpose of my question is to to guide me to the most efficient way to create a string from multiple variables in order to update a 4 line LCD display using LiquidCrystal\_I2C.h

Short, most efficient answer is: don't.

Longer version goes: don't create a string except in the output print buffer where your artfully created string goes anyway, it's 60 chars you don't have to allocate!

```arduino
LCD_4_Line.print( Day ); // I presume, snippet dint say. 
LCD_4_Line.print( '/' );
LCD_4_Line.print( Month );
LCD_4_Line.print( '/' );
LCD_4_Line.print( Year );
LCD_4_Line.print( ' ' );
LCD_4_Line.print( Hour );
LCD_4_Line.print( ':' );
LCD_4_Line.print( Minute );
LCD_4_Line.print( "pm" );
// and the finished string will be in the output buffer
// before the first char finished transmission
// less code and RAM to do the same thing... efficient?

```

And just for your own good: while you're in tiny memory space environments like most Arduino but for sure AVR-duino, stay clear of C++ String objects and every other C++ Container Class as well as C++ dynamic memory allocation. Avoid that like ebola.

AVR's and most other MCU's are tight spaces to code in. Sure, you can do what the big machines do.. just not fast or much at a time and practice Wasting Space won't show ways to get the most of the board and yourself.

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [August 11, 2026, 3:52pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/10 "2026-08-11T15:52:27Z")

</div>

Here is how I format the output from a DS3231 RTC.

```arduino
/ ********************************************************************
*
* Convert data/time to string
* 
********************************************************************* / 
String DtToString(const RtcDateTime& dt){
  char datestring[20];
  snprintf(datestring, 
      20,
      "%04u-%02u-%02u %02u:%02u:%02u",
      dt.Year(),
      dt.Month(),
      dt.Day(),
      dt.Hour(),
      dt.Minute(),
      dt.Second() );
   return String(datestring);
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 11, 2026, 4:11pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/11 "2026-08-11T16:11:41Z")

</div>

Why format an array of chars then cast it to a String ?

---

<div class="post-metadata">

**Author:** ![oldcurmudgeon](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oldcurmudgeon/32/1280882_2.png) [@oldcurmudgeon](https://forum.arduino.cc/u/oldcurmudgeon)\
**Post date:** [August 11, 2026, 4:26pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/12 "2026-08-11T16:26:32Z")

</div>

The callers are using it as part of other String processing. It is not on an UNO R3 - more ram is available. On an R3 it would probably be better to use a C string for the output and pass a pointer to it.

---

<div class="post-metadata">

**Author:** ![grantkbrown](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grantkbrown/32/1276968_2.png) [@grantkbrown](https://forum.arduino.cc/u/grantkbrown)\
**Post date:** [August 11, 2026, 5:17pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/13 "2026-08-11T17:17:06Z")

</div>

Thank you everyone

You all got me going down the right track

so again, Thank you

Grant Brown

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [August 11, 2026, 5:31pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/14 "2026-08-11T17:31:19Z")

</div>

> [@grantkbrown](#):
>
> guide me to a better approach to this

Discard `Stiring()` and do not build "strings of characters."

Also, do not repeatedly print static information, for example, your "/", " ", ":", "pm" et c.).

Create a static template by placing the static information ONE TIME.

Position the cursor ONLY for displaying NEW INFORMATION.

```arduino
if (now.minute() != oldminute) // minute has changed
{
  oldminute = now.minute(); // store current minute
  position_cursor(MINUTE); // use a function to position minutes
  display_data(oldminute); // send current minute to be displayed
}

```

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [August 11, 2026, 6:37pm UTC](https://forum.arduino.cc/t/how-to-create-efficent-strings/1455403/15 "2026-08-11T18:37:11Z")

</div>

> [@oldcurmudgeon](#):
>
> pass a pointer to it.

Or, in a smaller context, cut yourself one global chracter array big enough to hold temporary values that get filled in by functions like _DtToString_(), and pass nothing but _dt_). If there is no global "now" concept like _dt_ holds the current _RtcDateTime&_ you might not even pass that.

a7
