Closed tankist-git-2 closed 1 month ago
Hi @tankist-git-2. This repository contains the codebase of the Arduino IDE application. It is a common misconception that the Arduino programming language is somehow implemented in the Arduino IDE implementation. It is not. So issues related to the Arduino programming language are off topic in this repository, since nothing can be done in this repository to resolve such problems. The issues would instead need to be submitted to the repository where the relevant code is hosted.
But in this case it is a user error. You need to study the documentation:
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/
Syntax
myString.substring(from)
myString.substring(from, to)
Parameters
myString
: a variable of typeString
.from
: the index to start the substring at.to
(optional): the index to end the substring before.
You assumed that the to
parameter is the length. It is not. So you have specified an end point of the substring at a position before the start point. This is undefined behavior so you can't expect any reasonable result. If you want to obtain a substring of length 1 starting at index 2
, you should use code something like this:
String s3 = "1.2.3,4";
byte from = 2;
String temp = s3.substring(from, from + 1);
Serial.print("substr = "); Serial.println(temp.c_str());
If you would like further assistance with using the Arduino programming language, you are welcome to post over on Arduino Forum:
https://forum.arduino.cc/c/using-arduino/programming-questions/20
Describe the problem
The substring method of String class drives me crazy. I've got an unexpected behavior when I tried to substring a part of my string.
To reproduce
I do the following code in setup function:
the result is:
substr = .
Expected behavior
I expected the result equals to "2" like in any other IDE, because of starting position in 2 ("2...") and the length is 1, so just symbol "2" should be copied.
substr = 2
Arduino IDE version
2.3.3
Operating system
Windows
Operating system version
Windows 10 22H2
Additional context
No response
Issue checklist