arduinowstring.h库功能函数详细介绍(代码片段)

perseverance52 perseverance52     2022-12-25     520

关键词:

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.
无返回值

java中的构造函数详细介绍

构造函数:也是功能,专门用于给对象进行初始化。格式:1.函数名和类名相同   2.没有返回值类型   3.没有具体返回值  4。构造函数有return语句,用于结束初始化,可以不写。  (构造函数私有化的... 查看详情

轻松入门微信小程序(超级详细)

...3-4、云存储(1)3-5、云存储(2)4、电影小程序案例4-1、功能介绍与环境搭建4-2、Vant组件库4-3、电影列表4-4 查看详情

stm32的问题

...征和资源讲解-管脚的配置灵活性讲解(即管脚的重映射功能)-微控制器功能框图讲解开发工具IAREWARM介绍STM32微控制器最小系统-电源电路讲解(芯片电源功能块及开发板电源电路设计讲解)-时钟电路讲解(芯片时钟树及开发板... 查看详情

arraylist详细介绍(代码片段)

...一个数组队列,提供了相关的添加、删除、修改、遍历等功能。ArrayList 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在ArrayList中,我们即可以通过元素的序号快... 查看详情

gtk+介绍(代码片段)

...列和线程的通用的函数库。Pango用来实现国际化和本地化功能的函数库。ATK一种平易近人的工具函数包,提供了快捷键服务为肢体有缺陷的人使用电脑提供了便利。GDK它为整个GTK 查看详情

arduinoesp8266sd库函数介绍(代码片段)

...D类的函数提供用户用于访问SD卡并处理其中文件和目录的功能。begin():用法:SD.begin()或者SD.begin(cspin);cspin(可选):连接到SD卡芯片选择线的引脚(CS);默认为SPI总线的硬件SS线);返回值:true或falseexists():用法:SD.exists(filename);... 查看详情

xilinxvivado的使用详细介绍:使用ip核(代码片段)

...器等)、信号处理(FFT、DFT、DDS等)。IP核类似编程中的函数库(例如C语言中的printf()函数),可以直接调用,非常方便,大大加快了开发速度。方式一:使用Verilog调用IP核这里简单举一个乘法器 查看详情

utittest和pytest中mock的使用详细介绍(代码片段)

Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代掉指定的Python对象,以达到模拟对象的行为。python3.3以前,mock是第三方库,需要安装之后才能使用。python3.3之后,mock作为标准库内置... 查看详情

字符串,列表,字典详细功能介绍

一、字符串classstr(basestring):"""str(object=‘‘)->stringReturnanicestringrepresentationoftheobject.Iftheargumentisastring,thereturnvalueisthesameobject."""defcapitalize(self):"""首字母变大写""""""S.capitalize 查看详情

mycat配置文件的详细介绍(代码片段)

...配置系统参数、用户信息、访问权限及SQL防火墙和SQL拦截功能等schema.xml:用于配置逻辑库、逻辑表相关信息rule.xml:如果使用了水平切分,就需要使用该文 查看详情

windowsapi钩子函数

...读者在阅读相应的Win32API函数时就能很快地了解它的具体功能和使用方法,便于更快地掌握该接口函数。本书是从事MicrosoftWindows操作系统开发和应用人员的必备参考书,也可作为大专院校相关专业师生自学、教学参考用书。《Vis... 查看详情

httpwatch功能详细介绍(代码片段)

来源:https://www.cnblogs.com/Chilam007/p/6947235.htmlHttpWatch是功能强大的网页数据分析工具,集成在IE工具栏,主要功能有网页摘要、cookies管理、缓存管理、消息头发送/接收,字符查询、POST数据、目录管理功能和报告输出。HttpWatch是一款... 查看详情

poi详细介绍

...Ruby版本。结构:HSSF-提供读写MicrosoftExcelXLS格式档案的功能。XSSF-提供读写MicrosoftExcelOOXMLXLSX格式档案的功能。HWPF-提供读写MicrosoftWordDOC97格式档案的功能。XWPF -提供读写MicrosoftWo 查看详情

xilinxvivado的使用详细介绍:使用ip核(代码片段)

...器等)、信号处理(FFT、DFT、DDS等)。IP核类似编程中的函数库(例如C语言中的printf()函数),可以直接调用,非常方便,大大加快了开发速度。使用Verilog调用IP核这里简单举一个乘法器的IP核使用实例,使用Verilog调用。首先新... 查看详情

python标准库之shutil高阶文件操作『详细』(代码片段)

...、对文件的操作👺四、对目录的操作🐔五、归档功能「压缩包」🚚六、查询输出终端的尺寸扩展:将shutil支持7z格式压缩包参考资料💌相关博客 查看详情

jquery的each()详细介绍

...结构简洁,不容易出错。each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组、多维数组、DOM,JSON等等在javaScript开发过程中使用$each可以大大的减轻我们的工作量。下面提一下each的几种常用的用法  ... 查看详情

透明加密软件功能应用详细介绍

红线隐私保护系统文件加密功能能够为各类型的电子文档文件提供高强度加密保护,简单好用的分享功能以及自动后台加密方式,帮助个人及企业更好的掌控自己的文件安全。将设计图纸、开发代码、财务信息、客户资料等重要... 查看详情

jquery的each()详细介绍

...结构简洁,不容易出错。each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组、多维数组、DOM,JSON等等在javaScript开发过程中使用$each可以大大的减轻我们的工作量。下面提一下each的几种常用的用法  ... 查看详情