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

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


✨在Arduino开发框架下,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.
无返回值
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Arduino “blinkermqtt.h”文件是一个用于连接到MQTT(Message Queuing Telemetry Transport)消息协议的文件。MQTT是一种轻量级的发布/订阅消息传输协议,可用于在物联网设备之间传递消息。 在Arduino开发环境中,用户可以使用“blinkermqtt.h”文件来轻松实现与MQTT服务器的连接和通信。该提供了一系列函数和方法,使得Arduino能够发布和订阅MQTT主题,并与其他设备进行实时通信。 使用“blinkermqtt.h”文件,用户可以配置MQTT服务器的地址和端口,并使用一组简单的函数来发送和接收MQTT消息。用户可以通过调用“connect”函数来连接到MQTT服务器,并使用“publish”函数发布消息到指定的主题上。还可以使用“subscribe”函数订阅特定的主题,并在接收到新消息时触发相应的回调函数。 此外,该还提供了一些其他功能,如断线重连机制和SSL加密选项,以增强与MQTT服务器之间的通信安全性和可靠性。 总结来说,Arduino “blinkermqtt.h”文件提供了一个便捷的方式,使得Arduino能够连接到MQTT服务器并进行实时通信。它帮助简化了开发者的编码工作,使得在物联网设备中实现MQTT通信变得更加容易和可靠。 ### 回答2: blinkermqtt.h 是一个用于 Arduino文件,它提供了与 Blinker MQTT 云平台进行通信的功能。Blinker MQTT 云平台是一个物联网平台,用于将 Arduino 设备连接到云端,实现远程监控和控制。 使用 BlinkerMQTT.h 文件,我们可以轻松地将 Arduino 设备连接到云平台。文件中包含了一些常用的功能,比如发送数据到云平台、接收云端指令和更新设备状态等。我们只需要简单地调用这些函数,就能够实现与云平台的通信。 例如,我们可以使用文件中的 `Blinker.begin()` 函数来初始化与云平台的连接。然后,使用 `Blinker.run()` 函数来保持与云平台的通信,这个函数通常会放在 Arduino 的 `loop()` 函数中。接着,我们可以使用 `Blinker.attachData()` 函数来绑定数据,并使用 `Blinker.connect()` 函数将数据发送到云平台。 除了发送数据,文件还提供了订阅和处理云端的指令的功能。我们可以使用 `Blinker.attachControl()` 函数来绑定回调函数,当云端发送指令时,回调函数会被执行。 总之,blinkermqtt.h 文件为 Arduino 提供了与 Blinker MQTT 云平台进行通信的便利工具。使用这个文件,我们可以快速地实现与云平台的连接,并进行数据的发送和接收,为物联网应用提供了一种简单可靠的解决方案。 ### 回答3: Arduino的blinkermqtt.h文件是一个用于与Blinker MQTT云平台进行通信的文件。Blinker MQTT是一个基于MQTT协议的智能物联网云平台,它提供了丰富的功能和工具,方便用户将Arduino等硬件设备连接到云端并实现远程监控和控制。 使用blinkermqtt.h文件,首先需要在Arduino IDE中引入该文件。然后,我们可以使用一些函数和方法来配置和操作Blinker MQTT云平台。例如,我们可以使用begin()函数来初始化Blinker MQTT,设置设备名称和设备密钥等参数;使用connect()函数连接到Blinker MQTT云平台;使用publish()函数发布消息到云端;使用attachData()函数将传感器数据关联到云端;使用widgetRead()函数读取云端发送的指令等。 通过使用blinkermqtt.h文件,我们可以轻松地实现Arduino与Blinker MQTT云平台之间的通信。这使得我们可以通过云端控制Arduino设备,并实时获取设备数据。例如,我们可以通过云端发送指令来控制LED灯的开关,或者获取温度传感器的数据并在云端进行监控。这种交互使得智能物联网应用变得更加便捷和灵活。 总之,Arduino的blinkermqtt.h文件是一个用于与Blinker MQTT云平台进行通信的文件,它提供了丰富的函数和方法,方便我们实现Arduino与云端的连接和数据交互。使用这个文件,我们可以轻松地实现远程监控和控制等智能物联网应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值