C++ string Class 使用
来源:互联网 发布:特蕾莎修女 英文 知乎 编辑:程序博客网 时间:2024/04/30 13:29
C++ string class
The C++ Standard Template Library (STL) contains a string class that is used in several computer science classes. In order to use the string class you should include the following statements:
#include <string>
using std::string;
The following examples assume these declarations and initial values for each:
string s = "abc def abc";
string s2 = "abcde uvwxyz";
char c;
char ch[] = "aba daba do";
char *cp = ch;
unsigned int i;
s = ch; s = cp; A string literal or a string variable or a character array can be assigned to a string variable. The last two assignments have the same effect. Subscript s[1] = 'c';
c = s[1]; Changes s to equal "acc def abc"
Sets c to 'b'. The subscript operator returns a char value, not a string value. Length i = s.length();
i = s.size(); Either example sets i to the current length of the string s Empty? if(s.empty()) i++;
if(s == "") i++; Both examples add 1 to i if string s is now empty Relational operators if (s < s2) i++; Uses ASCII code to determine which string is smaller. Here the condition is true because a space comes before letter d Concatenation
s2 = s2 + "x";
s2 += "x";
s = s2.substr(1,4);
s = s2.substr(1,50);
s.erase(4); Removes the five characters starting in position 4 of s. The new value of s is "abc bc".
Remove from position 4 to end of string. The new value of s is "abc ". Character array to string s = ch; Converts character array ch into string s. String to character array cp = s.c_str(); Pointer cp points to a character array with the same characters as s. Pattern matching
i = s.find("ab",4);
if(s.rfind("ab",4) != string::npos)
cout << "Yes" << endl;
Because of a bug in the bgunix version of the g++ compiler, writing a string variable under setw control doesn't work. You need to use the c_str member function, like this:
cout << setw(15) << s.c_str() << endl;
- C++ string Class 使用
- c/c++ splitting string class
- copy的使用...NSStringFromClass([string class])... NSTaggedPointerString
- C++string的使用
- C++string使用详解
- C++string使用
- String Class
- String Class
- class string
- 【C++】实现一个简洁版的class String
- StrVec and String Class Design(C++Primer 5th)
- C++String使用笔记【自用】
- Class.forName(String className)使用哪个类加载器?
- Gson 使用fromGson(String,.class)无法转成其他对象
- hibernate中使用new class a(class b,class c)查询数据
- C.Class
- [C++]Class
- The C++ String Class
- 快速利用xampp搭建php+j2ee环境+虚拟主机
- windows下mysql解压版Dos命令小记
- Swf 8/9 MetaData 探究
- JavaScript中对字符串中空格的处理
- C/C++头文件一览
- C++ string Class 使用
- 学习指针不可少的好文章 !
- 单片机C51编程规范
- Keil C51 vs 标准C
- 在C51中变量的空间分配几个方法
- 学习linux
- 业务人员用的软件=BPEL+SOA
- Hibernate数据加载——Session.get/load
- gcc使用的方法