arduino 字符串[] 测试

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// send an intro:
Serial.println("\n\nString Constructors:");
Serial.println();
}

void loop() {
// using a constant String:
String stringOne = "Hello String";
Serial.print("stringOne:");Serial.println(stringOne); // prints "Hello String"
stringOne[5]='_';
Serial.print("stringOne[5]='_':");Serial.println(stringOne); // prints "Hello_String"
stringOne[5]="+-"[0];
stringOne[6]="+-"[1];
Serial.print("stringOne[5:6:2]='+-':");Serial.println(stringOne); // prints "Hello+-tring"
// converting a constant char into a String:
stringOne = String('a');
Serial.println(stringOne); // prints "a"

// converting a constant string into a String object:
String stringTwo = String("This is a string");
Serial.println(stringTwo); // prints "This is a string"

// concatenating two strings:
stringOne = String(stringTwo + " with more");
// prints "This is a string with more":
Serial.println(stringOne);

// using a constant integer:
stringOne = String(13);
Serial.println(stringOne); // prints "13"

// using an int and a base:
stringOne = String(analogRead(A0), DEC);
// prints "453" or whatever the value of analogRead(A0) is
Serial.println(stringOne);

// using an int and a base (hexadecimal):
stringOne = String(45, HEX);
// prints "2d", which is the hexadecimal version of decimal 45:
Serial.println(stringOne);

// using an int and a base (binary)
stringOne = String(255, BIN);
// prints "11111111" which is the binary value of 255
Serial.println(stringOne);

// using a long and a base:
stringOne = String(millis(), DEC);
// prints "123456" or whatever the value of millis() is:
Serial.println(stringOne);

// using a float and the right decimal places:
stringOne = String(5.698, 3);
Serial.println(stringOne);

// using a float and less decimal places to use rounding:
stringOne = String(5.698, 2);
Serial.println(stringOne);

// do nothing while true:
while (true);

}

 

串口:

16:39:47.782 -> &⸮P
16:39:47.782 -> <⸮Fq⸮n⸮⸮⸮0u8⸮
16:39:47.913 ->
16:39:47.913 -> String Constructors:
16:39:47.913 ->
16:39:47.913 -> stringOne:Hello String
16:39:47.946 -> stringOne[5]='_':Hello_String
16:39:47.982 -> stringOne[5:6:2]='+-':Hello+-tring
16:39:48.027 -> a
16:39:48.027 -> This is a string
16:39:48.027 -> This is a string with more
16:39:48.067 -> 13
16:39:48.067 -> 0
16:39:48.067 -> 2d
16:39:48.067 -> 11111111
16:39:48.067 -> 71
16:39:48.067 -> 5.698
16:39:48.113 -> 5.70

 

posted @ 2021-11-06 16:45  chengwh  阅读(109)  评论(0)    收藏  举报