“String - String()”的版本间的差异
(以“:String {| border="0" cellpadding="20" width="100%" |- |width="100%" valign="top" align="left"| <font color="olivedrab" size="+3">'''String()'''...”为内容创建页面) |
|||
第11行: | 第11行: | ||
Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including: | Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including: | ||
− | + | - a constant string of characters, in double quotes (i.e. a char array) | |
− | + | - a single constant character, in single quotes | |
− | + | - another instance of the String object | |
− | + | - a constant integer or long integer | |
− | + | - a constant integer or long integer, using a specified base | |
− | + | - an integer or long integer variable | |
− | + | - an integer or long integer variable, using a specified base | |
− | + | - a float or double, using a specified decimal palces | |
Constructing a String from a number results in a string that contains the ASCII representation of that number. The default is base ten, so | Constructing a String from a number results in a string that contains the ASCII representation of that number. The default is base ten, so | ||
− | + | <pre style="color:dimgray"> | |
+ | String thisString = String(13); | ||
+ | </pre> | ||
gives you the String "13". You can use other bases, however. For example, | gives you the String "13". You can use other bases, however. For example, | ||
− | + | <pre style="color:dimgray"> | |
+ | String thisString = String(13, HEX); | ||
+ | </pre> | ||
gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary, | gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary, | ||
+ | <pre style="color:dimgray"> | ||
+ | String thisString = String(13, BIN); | ||
+ | </pre> | ||
gives you the String "1101", which is the binary representation of 13. | gives you the String "1101", which is the binary representation of 13. | ||
第54行: | 第61行: | ||
<font color="orange" size="+1">'''示例'''</font> | <font color="orange" size="+1">'''示例'''</font> | ||
− | |||
All of the following are valid declarations for Strings. | All of the following are valid declarations for Strings. | ||
− | |||
<pre style="color:dimgray"> | <pre style="color:dimgray"> | ||
第70行: | 第75行: | ||
String stringOne = String(millis(), DEC); // using a long and a base | String stringOne = String(millis(), DEC); // using a long and a base | ||
String stringOne = String(5.698, 3); // using a float and the decimal places | String stringOne = String(5.698, 3); // using a float and the decimal places | ||
− | |||
</pre> | </pre> | ||
2015年7月27日 (一) 16:56的最后版本
String()
Constructs an instance of the String class. There are multiple versions that construct Strings from different data types (i.e. format them as sequences of characters), including: - a constant string of characters, in double quotes (i.e. a char array) - a single constant character, in single quotes - another instance of the String object - a constant integer or long integer - a constant integer or long integer, using a specified base - an integer or long integer variable - an integer or long integer variable, using a specified base - a float or double, using a specified decimal palces Constructing a String from a number results in a string that contains the ASCII representation of that number. The default is base ten, so String thisString = String(13); gives you the String "13". You can use other bases, however. For example, String thisString = String(13, HEX); gives you the String "D", which is the hexadecimal representation of the decimal value 13. Or if you prefer binary, String thisString = String(13, BIN); gives you the String "1101", which is the binary representation of 13.
语法 String(val) String(val, base) String(val, decimalPlaces)
val: a variable to format as a String - string, char, byte, int, long, unsigned int, unsigned long, float, double base (optional) - the base in which to format an integral value decimalPlaces (only if val is float or double) - the desired decimal places
an instance of the String class
All of the following are valid declarations for Strings. 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 |
更多建议和问题欢迎反馈至 YFRobot论坛