C++字符串类
来源:互联网 发布:ipaf看书软件 编辑:程序博客网 时间:2024/06/10 00:13
Constructing a String (创建string对象)
Create an empty string using string’s no-arg constructor (用无参构造函数创建一个空字串)
string newString;//
Create a string object from a string value or from an array of characters(由一个字符串常量或字符串数组创建string对象):
string s1("Welcome");s1.append(" to C++"); //appends "to C++" to s1cout << s1 << endl; //s1 now becomes Welcome to C++string s2("Welcome");s2.append(" to C and C++",3,2);//appends "C" to s2cout << s2 << endl; //s2 now becomes Welcome Cstring s3("Welcome"); //appends " to C" to s3s3.append(" to C and C++",5); //s3 now becomes Welcome to Ccout << s3 << endl;string s4("Welcome"); s4.append(4,'G'); // appends "GGGG" to s4cout << s4 << endl; // s4 now becomes WelcomeGGGG
【Assigning a String(为字符串赋值)】【assign】
You can use several overload functions to assign new contents to a string
string s1("Welcome");s1.assign("Dallas"); // assigns "Dallas" to s1cout << s1 << endl; // s1 now becomes Dallasstring s2 ("Welcome");s2.assign("Dallas,Texas",1,3); //assign "all" to s2cout << s2 << endl; //s2 now become allstring s3("Welcome");s3.assign("Dallas,Texas",6); // assigns "Dallas" to s3cout << s3 << endl; //s3 now become Dallasstring s4("Welcome");s4.assign(4,'G'); //assigns "GGGG" to s4cout << s4 << endl; //s4 now becomes GGGG
【Functions at,clear,erase,and empty】
- at(index) : 返回当前字符串中index位置的字符
- clear( ): 清空字符串
- erase(index,n);删除字符串从index开始的n个字符
- empty( ):检测字符串是否为空
string s1("Welcome");cout << s1.at(3) << endl; // s1.at(3) return ccout << s1.erase(2,3) << endl; //s1 is now Wemes1.clear(); // s1 is now emptycout << s1.empty() << endl; // s1.empty returns 1(means true) //不特别指定的话不会输出bool值,而是输出1!!!
【Comparing Strings(比较字符串)】【compare】
string s1("Welcome");string s2("Welcomg");cout << s1.compare(s2) << endl; //s1-s2 return -2cout << s2.compare(s1) << endl; //return 2cout << s1.compare("Welcome") << endl; // return 0
【Obtaining Substrings(获取子串)】
【at( )函数用于获取一个单独的字符;而substr( )函数则可以获取一个子串】
string s1("Welcome");cout << s1.substr(0,1) << endl; //return W; 从0号位置开始的1位字符cout << s1.substr(3) << endl; //return come 从3号位置开始直到结尾cout << s1.substr(3,3) << endl; //return com 从3号位置开始的3个字符
【Searching in a String(搜索字符串)】
【find( )函数可以从一个字符串中搜索一个子串或者一个字符】
string s1("Welcome to HTML");cout << s1.find("co") << endl; //returns 3; 返回子串出现的第一个位置cout << s1.find("co".6) << endl; //returns -1 (-1表示不存在) //从6号位置开始查找子串出现的第一个位置cout << s1.find('o') << endl; // returns 4 返回字符出现的第一个位置cout << s1.find('o',6) << endl; //returns 9 从6号位置开始查找字符出现的第一个位置
【Inserting and Replacing Strings(插入和替换字符串)】
【insert( ):将某个字符/字符串插入到当前字符串的某个位置】
【replace( ):将本字符串从某个位置开始的一些字符替换为其他内容】
string s1("Welcome to HTML");//W e l c o m e _ t o _ H T M L \0 //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15s1.insert(11,"C++ and ");cout << s1 << endl; //s1 becomes Welcome to C++ and HTMLstring s2("AA");s2.insert(1,4,'B'); //在一号位置处连续插入4个相同字符cout << s2 << endl; //s2 becomes to ABBBBAstring s3("Welcome to HTML");s3.replace(11,4,"C++"); //从11号位置开始向后的4个字符替换掉,注意'\0'cout << s3 << endl; //returns Welcome to C++
【字符串的运算符 String Operators】
Operator Description
[] //用数组下标运算符访问字符串中的字符= //将一个字符串的内容复制到另一个字符串+ //连续两个字符串得到一个新串+= //将一个字符串追加到另一个字符串末尾<< //讲一个字符串插入一个流>> //从一个流提取一个字符串,分解符为空格或者空结束符==,!=,< //用于字符串比较(前一个ASCⅡ减后一个)<=,>,>=string s1 = "ABC"; // The = operatorstring s2 = s1; // The = operatorfor(int i = s2.size() - 1;i >= 0;i--) cout << s2[i]; // The [] operatorstring s3 = s1 + "DEFG"; // The + operatorcout << s3 << endl; //s3 becomes ABCDEFGs1+= "ABC"; cout << s1 << endl; //s1 becomes ABCABCs1 = "ABC";s2 = "ABE";cout << (s1 == s2) << endl; //Diaplays 0cout << (s1 != s2) << endl; //Displays 1cout << (s1 > s2) << endl; //Displays 0 cout << (s1 >= s2) << endl; //Displays 0cout << (s1 < s2) << endl; //Dispalys 1cout << (s1 <= s2) << endl; //Dispalys 1
- 【C++】 C++字符串类
- C++字符串完全指南 - 各种字符串类
- C++字符串完全指南 - 各种字符串类
- c 字符串处理函数,字符串类和字符串变量
- 收藏.C++.字符串封装类
- C++_String_类字符串操作
- 字符串数学公式计算类(C#)
- string类和c字符串
- C++_String_类字符串操作
- C字符串
- C++--字符串
- C字符串
- [C#] 字符串
- c字符串
- C++--字符串
- C-字符串
- C-字符串
- C:字符串
- centos ftp简单搭建
- Linux下怎么来利用crontab实现计划任务
- c语言入门之项目1.9——利用switch解决实际问题
- 利用数组和链表实现简单的哈希表
- centos 6.6 安装redis
- C++字符串类
- 《利用python 进行数据分析》要点记录
- 黑马程序员——Java内存结构
- 一份面试题的总结 1
- codeforces Simple Skewness
- iOS7后 导航栏中的 translucent 导致的视图frame的变化
- Hdu 1757 A Simple Math Problem【矩阵快速幂】
- 安卓studio导入项目步骤详解
- leetcode--Reverse Integer