当前位置: 首页 > wzjs >正文

达内网站开发学习培训常州模板网站建设

达内网站开发学习培训,常州模板网站建设,安徽做网站电话,微商需要做网站吗string 是C中常见的一个用于处理和操作字符串的类。一直有用它来存储字符串&#xff0c;今天来介绍介绍一下它的定义和一些基本用法吧。 1、头文件 #include <string>2、定义和初始化 #include <iostream> #include <string> using namespace std;int main…

string 是C++中常见的一个用于处理和操作字符串的类。一直有用它来存储字符串,今天来介绍介绍一下它的定义和一些基本用法吧。

1、头文件

#include <string>

 

2、定义和初始化

#include <iostream>
#include <string>
using namespace std;int main() {// 默认初始化,创建一个空字符串string s1; // 使用字符串字面量初始化string s2 = "Hello"; // 使用另一个字符串初始化string s3(s2); // 使用重复字符初始化string s4(5, 'a'); cout << "s1: " << s1 << endl;cout << "s2: " << s2 << endl;cout << "s3: " << s3 << endl;cout << "s4: " << s4 << endl;return 0;
}

 

 

3、基本操作

1) 字符串拼接

可以使用 + += 操作符进行字符串拼接

#include <iostream>
#include <string>
using namespace std;int main() {string s1 = "Hello";string s2 = " World";// 使用 + 操作符拼接string s3 = s1 + s2;// 使用 += 操作符追加s1 += s2;cout << "s3: " << s3 << endl;cout << "s1: " << s1 << endl;return 0;
}    

 

 

2)字符串长度

可以使用 size() 或者 length() 方法获取字符串的长度

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello";cout << "Length of s: " << s.size() << endl;cout << "Length of s: " << s.length() << endl;return 0;
}    

 

 

3)访问字符

可以通过 [ ] 操作符或者 at( ) 方法访问字符串中的字符

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello";cout << "First character: " << s[0] << endl;cout << "Second character: " << s.at(1) << endl;//试一下越界访问cout << "use []: " << s[6] << endl;cout << "use at(): " << s.at(6) << endl;return 0;
}    

 

[ ] 操作符不进行边界检查,越界访问返回空,而 at() 方法会进行边界检查,若越界会抛出 std::out_of_range 异常

3.4 字符串比较

可以使用 ==!=<> 等操作符对字符串进行比较(哇咔咔这真的比C语言方便太多了)

#include <iostream>
#include <string>
using namespace std;int main() {string s1 = "Hello";string s2 = "World";if (s1 == s2) {cout << "s1 and s2 are equal." << endl;} else {cout << "s1 and s2 are not equal." << endl;}return 0;
}    

 

 

4、常用成员函数

1)substr()

提取子字符串

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello World";string sub = s.substr(6, 5); // 从索引 6 开始,提取长度为 5 的子字符串cout << "result is: " << sub << endl;return 0;
}

 

2)find()

查找子字符串字符在字符串中的位置

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello World";size_t pos = s.find("World");if (pos != string::npos) {cout << "Found at position: " << pos << endl;} else {cout << "Not found." << endl;}return 0;
}    

 

 

3)erase()

删除字符串中的某一部分

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello World";s.erase(5, 6); // 从索引 5 开始,删除长度为 6 的子字符串cout << "After erase: " << s << endl;return 0;
}    

 

 

4)insert()

在字符串指定位置插入字符或子字符串

#include <iostream>
#include <string>
using namespace std;int main() {string str = "Hello World";// 在索引 5 处插入 ", "str.insert(5, ", ");cout << str << endl;return 0;
}

 

5)replace()

替换字符串中指定位置的字符或子字符串

#include <iostream>
#include <string>
using namespace std;int main() {string str = "Hello World";// 从索引 6 开始,替换长度为 5 的子字符串为 "C++"str.replace(6, 5, "C++");cout << str << endl;return 0;
}

 

6)reverse()

反转字符串中的字符顺序。在 <algorithm> 头文件中,需结合 std:: 使用

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;int main() {string str = "Hello";reverse(str.begin(), str.end());cout << str << endl;return 0;
}

 

 

7)clear()

清空字符串中的所有字符,使其长度变为 0

#include <iostream>
#include <string>
using namespace std;int main() {string str = "Hello";str.clear();cout << "Length after clear: " << str.length() << endl;return 0;
}

 

 

8)empty()

判断字符串是否为空,若为空则返回 true,否则返回 false

#include <iostream>
#include <string>
using namespace std;int main() {string str1 = "";string str2 = "Hello";cout << "str1 is empty: " << (str1.empty() ? "Yes" : "No") << endl;cout << "str2 is empty: " << (str2.empty() ? "Yes" : "No") << endl;return 0;
}

 

5、与 C 风格字符串的转换

1)string 转 C 风格字符串

可以使用 c_str() 方法获取指向以 '\0' 结尾的字符数组的指针

#include <iostream>
#include <string>
using namespace std;int main() {string s = "Hello";const char* cstr = s.c_str();cout << "C-style string: " << cstr << endl;return 0;
}    

 

2)C 风格字符串转 string

可以直接用 C 风格字符串初始化 string 对象

#include <iostream>
#include <string>
using namespace std;int main() {const char* cstr = "Hello";string s(cstr);cout << "string: " << s << endl;return 0;
}    

 

 

小结

string这个类所包含的一些成员函数在很大程度上便捷了我们的编程,譬如,运用C语言定义一个字符数组,需要运用 '\0' 作为字符串结束标志来表示这是一个字符串,但是C++有很多定义字符串的方式;又譬如在查找特定字符时,C语言需要运用 for 循环或者 while 循环来遍历地找到目标字符,而C++ 中的类 string 有特定的成员函数 find 来找到该目标字符。这不仅实现了代码的优化,还极大地缩短了编程时间。


文章转载自:

http://N8NxdOwY.qgxnw.cn
http://i0eZPvX1.qgxnw.cn
http://A2VOz8vo.qgxnw.cn
http://P2SdGxXb.qgxnw.cn
http://5V3iyUWj.qgxnw.cn
http://Zk2n7lXM.qgxnw.cn
http://zEEPawa4.qgxnw.cn
http://FsSyvPGT.qgxnw.cn
http://larhTB7n.qgxnw.cn
http://DaXKua3k.qgxnw.cn
http://CVoSIS4E.qgxnw.cn
http://NZ1QfgSK.qgxnw.cn
http://vfM2cLgu.qgxnw.cn
http://ocfTo7Gg.qgxnw.cn
http://t8ClpNXC.qgxnw.cn
http://oc4QtmAx.qgxnw.cn
http://71VyE1aa.qgxnw.cn
http://HWWRbEyH.qgxnw.cn
http://FjOP1iVx.qgxnw.cn
http://R80DItt3.qgxnw.cn
http://zBcnpJ9i.qgxnw.cn
http://npsaijQV.qgxnw.cn
http://bhbK970H.qgxnw.cn
http://SWN9skGP.qgxnw.cn
http://cnfUGvfp.qgxnw.cn
http://UqVVRH21.qgxnw.cn
http://agG1QqYg.qgxnw.cn
http://QvAQ9gNU.qgxnw.cn
http://ZxzAl9wo.qgxnw.cn
http://dVmXg4o4.qgxnw.cn
http://www.dtcms.com/wzjs/623622.html

相关文章:

  • 兰州百度公司网站建设重庆合川企业网站建设联系电话
  • 网站如何做微信支付宝支付wordpress类目权限
  • 整站优化要多少钱深圳网站建设选哪家
  • 做网站开发的方案职业生涯规划ppt免费模板
  • 永州做网站公司做外贸网站市场
  • 威海城乡建设局网站首页导购网站模板
  • 做网站 所需资源wordpress禁止s.w.org
  • 临海网站建设公司西安最新数据消息
  • 赞叹天河网站建设公司河南省建设厅代建中心
  • 网站建设培训会讲话wordpress怎么分享到微信
  • 长春网站优化流程济南软件外包
  • 北京网站优化服务有限公司为什么做网站必须要用域名
  • 网站不备案不能用吗建设网银官网
  • jsp电商网站开发流程图荆门住房建设厅网站
  • 亚马逊的网站建设企业的网络推广
  • html可以做网站吗怎样宣传一个网站
  • 网站换行代码网页版qq音乐在线登录
  • 网站建设包含seo吗如何搭建公司网络
  • 湖北企业网站建设公司今天军事新闻最新消息视频
  • 无锡做网站公司多少钱呼和浩特网站建设
  • 法律顾问 网站 源码黑龙江省关于城市建设政策网站
  • 那种漂亮的网站怎么做设计师需要学历吗
  • 网站流量查询工具网页游戏奥奇传说
  • 长沙网页制作模板的网站wordpress 分类 id
  • 提供网站建设找哪家公司好wordpress多个博客
  • 标签化网站二级域名购买平台
  • 网站开发体会如何做网站建设团队建设
  • 电子商务网站建设信息智慧团建网站登录入口电脑版
  • 蓝色网站后台官网设计费用报价
  • 整站优化该怎么做炫酷的wordpress插件