- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <sstream>
- #include <vector>
- using namespace std;
- vector<string> split(const string& src, const string sp) ;
- int main() {
- string str("Hello,World");
- //1.获取字符串的第一个字母
- cout << "1.获取字符串的第一个字母:" + str.substr(0, 1) << endl;
- //2.获取字符串的第二和第三个字母
- cout << "2.获取字符串的第二和第三个字母:" + str.substr(1, 2) << endl;
- //3.获取字符串的最后一个字母
- cout << "3.获取字符串的最后一个字母:" + str.substr(str.length() - 1, 1) << endl;
- //4.字符串开头字母判断
- if (str.find("Hello") == 0) {
- cout << "4.字符串开头字母比较:以Hello开头" << endl;
- }
- //5.字符串结尾字母判断
- string w("World");
- if (str.rfind(w) == str.length() - w.length()) {
- cout << "5.字符串结尾字母比较:以World结尾" << endl;
- }
- //6.获取字符串长度
- stringstream ss;
- ss << str.length();
- cout << "6.获取字符串长度:" + ss.str() << endl;
- //7.大小写转换
- transform(str.begin(), str.end(), str.begin(), ::toupper);
- cout << "7.大小写转换:" + str;
- transform(str.begin(), str.end(), str.begin(), ::tolower);
- cout << "," + str << endl;
- //8.字符串转int,int转字符串
- int num;
- stringstream ss2("100");
- ss2 >> num;
- stringstream ss3;
- ss3 << num;
- cout << "8.字符串转int,int转字符串:" + ss3.str() + "," + ss3.str() << endl;
- //9.分割字符串
- vector<string> strs = ::split(str,string(","));
- cout << "9.分割字符串:[" + strs[0] +","+strs[1]<<"]"<<endl;
- //10.判断字符串是否包含
- str="Hello,World";
- if (str.find("o,W")!=-1) {
- cout << "10.分割字符串:包含o,W" << endl;
- }
- return 0;
- }
- vector<string> split(const string& src, string sp) {
- vector<string> strs;
- int sp_len = sp.size();
- int position = 0, index = -1;
- while (-1 != (index = src.find(sp, position))) {
- strs.push_back(src.substr(position, index - position));
- position = index + sp_len;
- }
- string lastStr = src.substr(position);
- if (!lastStr.empty())
- strs.push_back(lastStr);
- return strs;
- }
String操作篇-c++
原创
©著作权归作者所有:来自51CTO博客作者boostyu的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:String操作篇-python
下一篇:脚本改变生活(一)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++篇
C++的成长之路
c++ c语言 c++函数 c++程序 -
C++ String
C++ String
c++ 开发语言 后端 字符串 ci -
C++STL常用操作之string篇
STL简单使用之string篇简介:#include<string>一种数据类型,类似于字符串,但是可以进行一些比
数据结构 字符串 #include 赋值 -
C++篇汇总
C++篇汇总
C/C++ 封装 继承 多态 指针