String stringOne ="Hello String";// using a constant String
String stringOne =String('a');// converting a constant char into a String
String stringTwo =String("This is a string");// converting a constant string into a String object
String stringOne =String(stringTwo +" with more");// concatenating two strings
String stringOne =String(13);// using a constant integer
String stringOne =String(analogRead(0), DEC);// using an int and a base
String stringOne =String(45, HEX);// using an int and a base (hexadecimal)
String stringOne =String(255, BIN);// using an int and a base (binary)
String stringOne =String(millis(), DEC);// using a long and a base
String stringOne =String(5.698,3);// using a float and the decimal places
myString.compareTo(myString2)
参数
myString: a variable of type String.
myString2: another variable of type String.
返回值
一个负数: 如果myString出现在myString2之前.0:if String equals myString2.
一个正数: 如果myString在myString2后面.
concat():将参数附加到String对象。
myString.concat(parameter)
Parameters
myString: a variable of type String.
parameter: Allowed data types: String, string,char, byte,int,unsignedint,long,unsignedlong,float,double,__FlashStringHelper(F() macro).
Returns
true: success.
false:failure(in which case the String is left unchanged).
myString.c_str()
参数
myString: a variable of type String.
endsWith():测试一个String是否以另一个String的字符结尾。
myString.endsWith(myString2)
参数
myString: a variable of type String.
myString2: another variable of type String.
返回值
true:if myString ends with the characters of myString2.
false: otherwise.
myString.equalsIgnoreCase(myString2)
参数
myString: variable of type String.
myString2: variable of type String.
返回值
true:if myString equals myString2(ignoring case).
false: otherwise.
getBytes():将String的字符复制到所提供的缓冲区。
myString.getBytes(buf, len)
参数
myString: a variable of type String.
buf: the buffer to copy the characters into. Allowed data types: array of byte.
len: the size of the buffer. Allowed data types:unsignedint.
无返回值
myString.indexOf(val)
myString.indexOf(val, from)
参数
myString: a variable of type String.
val: the value to search for. Allowed data types:char, String.
from: the index to start the search from.
返回值
The index of val within the String, or -1if not found.
myString.lastIndexOf(val)
myString.lastIndexOf(val, from)
参数
myString: a variable of type String.
val: the value to search for. Allowed data types:char, String.
from: the index to work backwards from.
返回值
The index of val within the String, or -1if not found.
length():返回字符串的长度,以字符为单位。(注意,末尾不包含空字符。)
myString.length()
参数
myString: a variable of type String.
返回值
The length of the String in characters.
myString.remove(index)
myString.remove(index, count)
参数
myString: a variable of type String.
index: The position at which to start the remove process(zero indexed). Allowed data types:unsignedint.
count: The number of characters to remove.
Allowed data types:unsignedint.
示例:
String greeting ="hello";
greeting.remove(2,2);// greeting now contains "heo"
无返回值
myString.replace(substring1, substring2)
参数
myString: a variable of type String.
substring1: another variable of type String.
substring2: another variable of type String.
无返回值
reserve():函数的作用是:在内存中分配一个缓冲区来处理String对象。
myString.reserve(size)
参数
myString: a variable of type String.
size: the number of bytes in memory to save for String manipulation. Allowed data types:unsignedint.
无返回值
String myString;voidsetup(){// initialize serial and wait for port to open:
Serial.begin(9600);while(!Serial){;// wait for serial port to connect. Needed for native USB}
myString.reserve(26);
myString ="i=";
myString +="1234";
myString +=", is that ok?";// print the String:
Serial.println(myString);}voidloop(){// nothing to do here}
setCharAt():设置字符串的一个字符。对字符串现有长度以外的索引没有影响。
myString.setCharAt(index, c)
Parameters
myString: a variable of type String.
index: the index to set the character at.
c: the character to store to the given location.
无返回值
startsWith():测试一个String是否以另一个String的字符开头。
myString.startsWith(myString2)
参数
myString, myString2: a variable of type String.
返回值
true:if myString starts with the characters of myString2.
false: otherwise
myString.substring(from)
myString.substring(from, to)
参数
myString: a variable of type String.
from: the index to start the substring at.to(optional): the index to end the substring before.
返回值
The substring.
toCharArray():将String的字符复制到所提供的缓冲区。
myString.toCharArray(buf, len)
参数
myString: a variable of type String.
buf: the buffer to copy the characters into. Allowed data types: array of char.
len: the size of the buffer. Allowed data types:unsignedint.
无返回值