Questions tagged [string]

A sequence of characters -- including letters, numbers and symbols -- often used for representing information in a human-readable format.

Use this tag for any questions which relate directly to using text strings in Arduino programming, whether for input, output, storage, or transmission.

This tag can cover any representation of a string which Arduino will support, including char arrays (null-terminated or otherwise), the Arduino String class, and the C++ std::string class.

325 questions
71
votes
12 answers

How do I split an incoming string?

I am sending a list of servo positions via the serial connection to the arduino in the following format 1:90&2:80&3:180 Which would be parsed as: servoId : Position & servoId : Position & servoId : Position How would I split these values up, and…
ValrikRobot
  • 843
  • 2
  • 8
  • 6
55
votes
12 answers

How do I print multiple variables in a string?

Say I have some variables that I want to print out to the terminal, what's the easiest way to print them in a string? Currently I do something like this: Serial.print("Var 1:");Serial.println(var1); Serial.print(" Var…
sachleen
  • 7,565
  • 5
  • 40
  • 57
17
votes
1 answer

Convert int to char[]

I'm looking to convert an int value to a char array. currently I've found the following will return [number] int num = [number] str = String(num); str.toCharArray(cstr,16); Serial.println(cstr); However, per Majenko's The Evils of Arduino Strings I…
ATE-ENGE
  • 941
  • 3
  • 19
  • 32
12
votes
6 answers

Using String instead of C string, yet another attempt to touch a loaded issue

I've been reading a lot over the years why we should not use the notorious String class and how heap fragmentation is bad practice and not professional and we should never use it in our programs or we'll never enter the C hall of fame and get to be…
Nino
  • 411
  • 4
  • 9
10
votes
5 answers

formatting strings in Arduino for output

I wander what is the best option for formatting strings in Arduino for output. I mean what's a preferable way in the point of view of performance, memory usage – things like that. I see, people usually use direct Serial.print/println, like this: …
zhekaus
  • 459
  • 2
  • 6
  • 18
10
votes
1 answer

Arduino Convert std:string to String

I'm working on BLE project with espressif library. And It returns me founded BLE device. std::string getManufacturerData(); std::string getName(); int getRSSI(); BLEScan* getScan(); When I want to print device name to serial port…
BK52
  • 203
  • 1
  • 2
  • 4
9
votes
3 answers

Creating formatted String (including floats) in Arduino-compatible C++

I'm using a Mega2560 and a generic SSD1306 OLED display with the Adafruit_SSD1306 (and by extension, Adafruit_gfx) library. I need to print a bunch of float values (ranging between 30.0 and 99.9) to the display with exactly one decimal place of…
Bitbang3r
  • 561
  • 1
  • 3
  • 14
8
votes
3 answers

Why is 'n' parameter of snprintf ignored?

I have found that the n parameter of snprintf() seems to be ignored in my code. char asdf[10]; Serial1.println(snprintf(asdf, 2, "hello")); This prints 5 when I would expect it to print 2. What is happening?
Westin
  • 83
  • 4
8
votes
1 answer

Is it better to use c_str or toCharArray?

When reading/trying a recent answer, I was surprised to see that Arduino's String class supports the c_str() method, just like the C++ std::string class. As expected, it appears to get a pointer to the string's contents as a null-terminated char…
Peter Bloomfield
  • 10,982
  • 9
  • 48
  • 87
8
votes
2 answers

Convert a Bitstring into an integer value

I have got a String filled up with 0 and 1 and would like to get an Integer out of it: String bitString = ""; int Number; int tmp; bitString = ""; for (i=1;i<=10;i++) { tmp= analogRead (A0); bitString += tmp % 2; delay(50); } //…
kimliv
  • 561
  • 1
  • 4
  • 17
7
votes
7 answers

Understanding why should avoid “String” and alternative solutions

Why “Strings” are bad for Arduino? Which is the most efficient and fastest solution to read and store the data from Accelerometer and GPS? Stings are evil for Arduino An Uno or other ATmega328-based board only has 2048 bytes SRAM. The SRAM is…
Andrea Ciufo
  • 235
  • 1
  • 3
  • 9
6
votes
1 answer

Serial.write and Serial.println return different values? Cannot use correct ones

I have a text file on an SD card, and I am trying to print the lines to the serial monitor. This Code Works: #include #include File myFile; void setup() { Serial.begin(9600); Serial.print("Initializing SD card..."); …
user3151828
  • 65
  • 1
  • 7
6
votes
3 answers

Arduino String memory allocation

When a String variable is declared and modified inside a function, where does it go? To the heap(as string declaration is a part of dynamic memory allocation) or to the stack(as it is a part of function)? And is the space regained after the function…
goddland_16
  • 529
  • 5
  • 14
6
votes
1 answer

Format Integer in Arduino

How to format an integer as 3 digit string? If I give 3 formated string should be 003 printf("%03d\n", 3); // output: 003 This doesn't work in Arduino.
user119o
  • 225
  • 5
  • 10
6
votes
3 answers

How to multiply strings?

I want to have a line like this Serial.println(count*"B"); So if count=6, it would print BBBBBB, etc. Is there anything like this for arduino?
Mirror318
  • 163
  • 1
  • 5
1
2 3
21 22