Arduino里的字符串笔记 下

Arduino里的字符串笔记


1 字符串转数字 String To Int

/*
使用String.toInt()将字符串转为数字示例
 */

String inString = "";    // string to hold input

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 toInt():");
  Serial.println();
}

void loop() {
  // Read serial input:
  while (Serial.available() > 0) {
    int inChar = Serial.read();
    if (isDigit(inChar)) {
      // convert the incoming byte to a char
      // and add it to the string:
      inString += (char)inChar;
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\n') {
      Serial.print("Value:");
      Serial.println(inString.toInt());
      Serial.print("String: ");
      Serial.println(inString);
      // clear the string for new input:
      inString = "";
    }
  }
}

2 数字转字符串 IntTo String

  1. 直接赋值
String myNumber = 1234;
  1. 使用类成员函数转换,将数字自动追加到字符串结尾
int value = 123;
String myReadout = "The reading was ";
myReadout.concat(value);
  1. 使用类运算符转换,将数字自动追加到字符串结尾
int value = 123;
String myReadout = "The reading was ";
myReadout += value;

3 常见函数整理

String.c_str()
一个将string转换为 const* char的函数。
c_str函数的返回值是const char的,不能直接赋值给char

    String s = "Chelse";  
    const char *str = s.c_str();  
strcpy
原型声明:char *strcpy(char* dest, const char *src);

strcpy(A, B.c_str());//将B复制给A
sprintf
把整数123 打印成一个字符串保存在s 中。

char *s;
sprintf(s, "%d", 123); //产生"123"

4 char*,const char*和string 三者转换

1. const char* 和string 的转换

(1) const char*转换为 string,直接赋值即可。

const char* tmp = "tsinghua"
string s = tmp;

(2) string转换为const char*,利用c_str()

string s = "Arduino";
const char*tmp = s.c_str();
2. char*和const char* 的转换

(1) const char*转化为char*,利用const_cast<char*>

const char* tmp = "Arduino";
char* p = const_cast<char*>(tmp);

(2) char*转化为const char*,直接赋值即可。

char* p = "tsinghua"
const char* tmp = p;
3. char*和string 的转换

有了1和2的基础,char*和string转化就很简单了。

(1)char*转化为string,直接赋值即可。

char* p = "Arduino"
string str = p;

(2)string转化为char*,走两步,先是string->const char*,然后是const char*->char*

string str = "Arduino";
char* p = const_cast<char*>(str.c_str());
Arduino字符串相关笔记:https://www.cnblogs.com/dapenson/p/16998008.html
  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Arduino中,可以使用字符串截取函数来截取字符串的某一位置。这个函数的参数包括原始字符串、要截取的字符串的起始索引和截取字符串的结束索引。函数将返回截取后的字符串。 另外,还可以使用字符串查找函数来在字符串中查找特定的字符或字符串。这个函数的参数包括原始字符串、要查找的特定字符或字符串以及查找的起始索引。如果成功找到,函数将返回特定字符或字符串的位置;如果未找到,函数将返回-1。 另外,还可以使用分隔符来分割字符串数据。例如,如果有一个数据为"123|456|78|9"的字符串,可以使用分隔符"|"将数据分割成"123"、"456"、"78"和"9"四组数据,以便进行数据的提取和处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [字符串数据处理 arduino](https://blog.csdn.net/xuqingyuxu/article/details/126621089)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Arduino 分割字符串库,strFenGe.rar](https://download.csdn.net/download/qq_41909142/71978349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dapenson

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值