Arduino内置教程-字符串-String Constructors

字符串对象结构

  • String object允许你用多种方法操作文本的字符串。你可以往字符串里增加字符,组合字符串,获得字符串长度,搜索和替换子字符串,等等。这个教程示范怎么初始化串口对象。
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
  • 所有的函数都可以用来声明串口对象。它们都会得出一个对象(包括用任何String函数操作一个字符串的字符)。观察它们运行,更新下面代码到Arduino 或者 Genuino 开发板上,并打开 Arduino IDE 串口监视器。你会看到每个声明的结果。对比每个用println() 打印出来的结果和上面的声明。

硬件要求

  • Arduino or Genuino 开发板

电路

  • 这个例子不需要连接额外的电路,除了你的开发板需要连接到你的电脑,并且打开Arduino IDE的串口监视器窗口。

请输入图片描述
图由 Fritzing 软件绘制

样例代码

/*
   String constructors

 Examples of how  to create strings from other data types

 created 27 July 2010
 modified 30 Aug 2011
 by Tom Igoe

 http://www.arduino.cc/en/Tutorial/StringConstructors

 This example code is in the public domain.
 */

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.println(stringOne);      // prints "Hello String"

  // 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);

}

[Get Code]

更多

  • String object – 字符串对象的参考
  • CharacterAnalysis - 使用operators来识别对应的特征类型。
  • StringAdditionOperator - 用不同方法把字符串加到一起。
  • StringAppendOperator - 用+=运算符和concat()方法来添加东西到字符串里。
  • StringCaseChanges - 改变字符串的状态。
  • StringCharacters - 在字符串里获得或设置一个指定的字符的值
  • StringComparisonOperators - 按字母排列顺序地比较字符串
  • StringConstructors - 初始化字符串对象
  • StringIndexOf - 寻找在字符串里字符的第一个或最后一个的状态
  • StringLength - 获得和修剪字符串的长度
  • StringLengthTrim - 获得和修剪字符串的长度
  • StringReplace - 替换字符串里的个别字符
  • StringStartsWithEndsWith - 检查一个给定的字符或子串(substrings)的开始或结尾
  • StringSubstring - 在给定的字符串里寻找"phrases"
  • StringToInt - 允许你把字符串转换成整数数字

标签: arduino内置教程, arduino string constructors