| 1 |
/* |
| 2 |
String charAt() and setCharAt() |
| 3 |
|
| 4 |
Examples of how to get and set characters of a String |
| 5 |
|
| 6 |
created 27 Jul 2010 |
| 7 |
modified 2 Apr 2012 |
| 8 |
by Tom Igoe |
| 9 |
|
| 10 |
This example code is in the public domain. |
| 11 |
|
| 12 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/StringCharacters |
| 13 |
*/ |
| 14 |
|
| 15 |
void setup() { |
| 16 |
// Open serial communications and wait for port to open: |
| 17 |
Serial.begin(9600); |
| 18 |
while (!Serial) { |
| 19 |
; // wait for serial port to connect. Needed for native USB port only |
| 20 |
} |
| 21 |
|
| 22 |
Serial.println("\n\nString charAt() and setCharAt():"); |
| 23 |
} |
| 24 |
|
| 25 |
void loop() { |
| 26 |
// make a String to report a sensor reading: |
| 27 |
String reportString = "SensorReading: 456"; |
| 28 |
Serial.println(reportString); |
| 29 |
|
| 30 |
// the reading's most significant digit is at position 15 in the reportString: |
| 31 |
char mostSignificantDigit = reportString.charAt(15); |
| 32 |
|
| 33 |
String message = "Most significant digit of the sensor reading is: "; |
| 34 |
Serial.println(message + mostSignificantDigit); |
| 35 |
|
| 36 |
// add blank space: |
| 37 |
Serial.println(); |
| 38 |
|
| 39 |
// you can also set the character of a String. Change the : to a = character |
| 40 |
reportString.setCharAt(13, '='); |
| 41 |
Serial.println(reportString); |
| 42 |
|
| 43 |
// do nothing while true: |
| 44 |
while (true); |
| 45 |
} |