Arduino WString.h库功能函数详细介绍

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino WString.h库功能函数详细介绍相关的知识,希望对你有一定的参考价值。

Arduino WString.h库功能函数详细介绍


String:是一个很重要的数据类型。

  • String数据转换函数
String(val)
String(val, base)
String(val, decimalPlaces)

参数说明
val:要格式化为String类型的变量。允许的数据类型:string, char, byte, int, long, unsigned int, unsigned long, float, double。
base:(可选)格式化一个整数值的基数。
decimalPlaces:仅当val是float或double时。小数点后几位。

  • 转换示例
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

功能函数

  • charAt():访问字符串的特定字符。
myString.charAt(n)//n: 变量为 unsigned int类型.

  • compareTo():比较两个字符串,测试一个在另一个之前还是之后,或者它们是否相等。字符串使用字符的ASCII值逐个字符进行比较。这意味着,例如,a在b之前,但在a之后。数字在字母之前。
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, unsigned int, long, unsigned long, float, double, __FlashStringHelper(F() macro).

Returns
true: success.
false: failure (in which case the String is left unchanged).

  • c_str():将字符串的内容转换为char*、以空结尾的字符串。注意,这提供了对内部String缓冲区的直接访问,应该谨慎使用。特别是,永远不要通过返回的指针修改字符串。当您修改String对象时,或当它被销毁时,之前由c_str()返回的任何指针都将无效,不应再使用。
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.

  • equals():比较两个字符串是否相等。比较是区分大小写的,这意味着字符串“hello”不等于字符串“hello”。
myString.equals(myString2)

参数
myString, myString2: variables of type String.

返回值
true: if string equals string2.
false: otherwise.

  • equalsIgnoreCase():比较两个字符串是否相等。比较不区分大小写,这意味着String(“hello”)等于String(“hello”)。
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: unsigned int.

无返回值

  • indexOf():在另一个String中定位一个字符或String。默认情况下,搜索从String的开头开始,但也可以从给定的索引开始,允许查找字符或String的所有实例。
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 -1 if not found.

  • lastIndexOf():在另一个String中定位一个字符或String。默认情况下,从String的末尾进行搜索,但也可以从给定的索引进行反向搜索,从而定位字符或String的所有实例。
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 -1 if not found.

  • length():返回字符串的长度,以字符为单位。(注意,末尾不包含空字符。)
myString.length()

参数
myString: a variable of type String.

返回值
The length of the String in characters.

  • remove():原地修改一个String,从提供的索引中删除字符到String的末尾,或者从提供的索引中删除字符到index + count。
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: unsigned int.
count: The number of characters to remove.
 Allowed data types: unsigned int.
 示例:
String greeting = "hello";
greeting.remove(2, 2);  // greeting now contains "heo"
无返回值

  • replace():函数的作用是:使用另一个字符替换一个给定字符的所有实例。您还可以使用replace将String的子字符串替换为不同的子字符串。
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: unsigned int.
无返回值

String myString;
void setup() {
  // 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);
}

void loop() {
  // 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

  • substring():获取一个字符串的子字符串。起始索引是包含的(对应的字符包含在子字符串中),但可选的结束索引是排他的(对应的字符不包含在子字符串中)。如果省略结尾索引,子字符串将继续到字符串的末尾。
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: unsigned int.
无返回值

  • toDouble():将有效的String转换为double。输入字符串应该以数字开始。如果String包含非数字字符,函数将停止执行转换。例如,字符串123.45、123、123fish分别被转换为123.45、123.00、123.00。请注意,“123.456”近似于123.46。还要注意,浮点数只有6-7个精确的十进制数字,更长的字符串可能会被截断。
myString.toDouble()

参数
myString: a variable of type String.

返回值
如果因为String不是以数字开头而不能执行有效的转换,则返回0。数据类型:double

  • toInt():将有效的字符串转换为整数。输入字符串应该以整数开始。如果String包含非整数,函数将停止执行转换。
myString.toInt()

Parameters
myString: a variable of type String.

返回值
如果因为String不是以整数开头而不能执行有效的转换,则返回0。 数据类型: long.

  • toFloat():将有效的字符串转换为浮点数。输入字符串应该以数字开始。如果String包含非数字字符,函数将停止执行转换。例如,字符串123.45、123、123fish分别被转换为123.45、123.00、123.00。请注意,“123.456”近似于123.46。还要注意,浮点数只有6-7个精确的十进制数字,更长的字符串可能会被截断。
myString.toFloat()

Parameters
myString: a variable of type String.

返回值
如果因为String不是以数字开头而不能执行有效的转换,则返回0。 数据类型: float.

  • toLowerCase():获取字符串的小写版本。从1.0开始,toLowerCase()修改String,而不是返回一个新的。
myString.toLowerCase()

Parameters
myString: a variable of type String.
无返回值

  • toUpperCase():获取字符串的大写版本。从1.0开始,toUpperCase()修改String,而不是返回一个新的。
myString.toUpperCase()

参数
myString: a variable of type String.
无返回值

  • trim():获取一个删除了任何前导和尾随空格的String版本。从1.0开始,trim()在适当的地方修改String,而不是返回一个新的String。
myString.trim()

参数
myString: a variable of type String.
无返回值

以上是关于Arduino WString.h库功能函数详细介绍的主要内容,如果未能解决你的问题,请参考以下文章

Arduino 基于Serial类的函数详细说明

如何使用Arduino的库?

Arduino-IIC-Wire.h

Arduino ESP8266 SD库函数介绍

ESP32/8266 + Arduino SSD1306库驱动OLED

arduino五针 HC-SRF05超声波测距模块/超声波传感器HC-SRF05资料越详细越好