String常用功能

1.String属性

2.String方法

构造方法:

1.String(byte bytes[],int offset,int length,String charsetName)
参数:1byte数组;2起始下标;3数组长度;4字符编码(如utf-8)
逻辑:首先判断4不为空,123的长度为正。。。。。。

方法:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C++中string常用功能: 一、字符串赋值 1. 使用赋值运算符=进行字符串赋值 ```cpp string str1 = "hello"; string str2 = str1; ``` 2. 使用构造函数进行字符串赋值 ```cpp string str1("hello"); string str2(str1); string str3("hello", 3); // str3的值为"hel" string str4(5, 'a'); // str4的值为"aaaaa" ``` 3. 使用assign函数进行字符串赋值 ```cpp string str1; str1.assign("hello"); string str2; str2.assign("hello", 3); // str2的值为"hel" string str3; str3.assign(str1); string str4; str4.assign(5, 'a'); // str4的值为"aaaaa" ``` 二、字符串拼接 1. 使用加号+进行字符串拼接 ```cpp string str1 = "hello"; string str2 = "world"; string str3 = str1 + " " + str2; // str3的值为"hello world" ``` 2. 使用append函数进行字符串拼接 ```cpp string str1 = "hello"; string str2 = "world"; str1.append(" "); str1.append(str2); // str1的值为"hello world" ``` 三、字符串比较 1. 使用比较运算符进行字符串比较 ```cpp string str1 = "hello"; string str2 = "world"; if (str1 == str2) { // 两个字符串相等 } else if (str1 < str2) { // str1小于str2 } else { // str1大于str2 } ``` 2. 使用compare函数进行字符串比较 ```cpp string str1 = "hello"; string str2 = "world"; int result = str1.compare(str2); if (result == 0) { // 两个字符串相等 } else if (result < 0) { // str1小于str2 } else { // str1大于str2 } ``` 四、字符串查找 1. 使用find函数进行字符串查找 ```cpp string str = "hello world"; int pos = str.find("world"); // pos的值为6 ``` 2. 使用rfind函数进行反向字符串查找 ```cpp string str = "hello world"; int pos = str.rfind("l"); // pos的值为9 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值