您的位置:首页 > 编程语言 > C语言/C++

在C++中string类用法

2015-06-01 19:22 501 查看
头文件机命名空间::

#include<string>

using namespace std;

遍历使用下标类型:

string::size_type类型描述的是string类型中下标的类型。所以遍历string类型的方法是:

for(string::size_type i = 0; i < obj.size(); i++)


//因为长度不确定,所以i的类型就没办法确定,只能用string::size_type类型。

{

cout << obj[i] <<endl; //变量string对象的每一个成员。

}


obj.c_str()返回的const char *类型的指针,表明不能最这段空间通过其他方式赋值,即不能成为左值。起到保护做用。

重要:

obj[i]表示obj字符串中的第i+1个字符,注意obj[i]是可以做为左值的。 //非常重要。

注意:

在某些情况下,string类就相当于一个字符容器。string类型也支持迭代器操作。

string 类的常用操作接口:

string类的更多操作::

1、创建string 对象的方法:

string str;


string str("hello world");


string str="hello world";

string str(obj); string str = obj;

2、将string类中字符串的首地址返回: (返回string类型字符串的首地址,即和C字符串的转换, c_str)

const char *p;

p = str.c_str();

3、判断string类对象是否为空: (判断字符串是否为空, empty)

string str;

str.empty(); //为空返回true,否则返回false;

字符串长度与字符串存储能力
4、获取string类对象中字符的个数: (字符串预设长度,size()和length,strlen(*.c_str()))

string::size_type size;
//注意必须是size_type类型。(string的下标类型是size_type类型)


str.resize(number); //预设字符串中将存储100个字符。但并不是实际就100个,也并不是说实际只能存放100个。

size = str.size(); //获取字符串str的预设长度,字符串长度可能并没有这么长。

size = str.length(); //获取字符串str的预设长度,字符串本身可能并没有这么长。

举例:

string str;

str.resize(100);

str[0]='a';

str[1]='b';

str[2]='\0';

cout << str.size() << endl;
//运行结果是100


cout<< str.length()<<endl;
//运行结果是100


cout<<strlen(str.c_str())<<endl;
//运行结果是2,字符串的真正长度。





5、字符串连接的两种方法: (字符串连接,与追加 + 和 append)

方法1:

string str1("hello");

string str2("world");

str1
+= str2; //还可以是 str1 += "world";也是可以的。


方法2:

str1.append(str2);


6、向字符串中插入一个字符串: (插入一个字符串, insert)

string str("hello world");

str.insert(pos,"billchen");


在str字符串中下标为pos的位置插入字符串"billchen"。


注意:


替换子串replace相当于erase删除子串,然后在insert插入子串。



7、删除字符串中的一个子串: (删除子串, erase)

string str("hello world");

str.erase(pos,len);

将str字符串中下标从pos开始的长度为len的一个子串删除。


注意:


清空string字符串,使用clear()方法。

str.clear();




8、替换字符串中的一个子串: (替换子串, replace)

string str1("hello world");

str1.replace(pos,len,args);


将str1字符串中下标从pos开始的长度为len的一个子串删除,使用args指向的字符串代替。




9、查找字符串中某个子串出现的位置: (在字符串中查找指定的字符串, find)

string str("hello world");

str.find("wo"); //在str中查找"wo"第一次出现的位置的下标。

str.rfind("wo"); //在str中查找"wo"最后一次出现位置的下标。

string::size_type ops = str.find("wo");

if(ops == string::npos)

{

cout << "没有找到" << endl;

}

相当与C语言中的strchr和strrchr的意思。

str.find_first_of(args); //在str中查找args的任意字符的第一次出现。

str.find_last_of(args); //在str中查找args的任意字符的最后一次出现。

str.find_first_no_of(args); //在str中查找第一个不属于args的字符。

str.find_last_no_of(args); //在str中查找最后一个不属于args的字符。

注意:

string 类的find方法的返回值都是size_type类型。

当没有查到时,返回值是string::npos; 可以将返回值和string::nops比较,判断是否找到。

遍历

方法1:下标法

11、遍历string的每一个成员:

string str("hello world");

string::size_type i;

for( i=0; i <= str.size(); i++)

{

cout << str[i] << " ";

}

方法1:迭代器

11、遍历string的每一个成员:

string str("hello world");

string::iterator iter = str.begin();

while(iter != str.end())

{

cout << *iter << " ";

iter++;

}

截取子串

12、从字符串中截取子串: (截取子串, substr)

string str("hello world");

str.substr(pos,n); //截取下标从pos开始,长度为n的一个子串。

str.substr(pos); //截取下标从pos开始到字符串末尾的子串。

字符串关系比较

13、字符串之间的关系的判断。

是否相等 ==

是否不等 !=

大于,小于,等关系运算符保持原来的含义。

C++的string类同时提供了compare方法,用来判断字符串之间的关系。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: