c++字符串操作

字符串是我们编程中最重要的元素,所以对字符串的操作尤其重要,我在这里主要总结了string这个类中的一些对字符串操作的方法。

  1. string构造函数
    string str; //生成一个空字符串str
    string s(str); //将str拷贝给s
    string s(str,index);//将str中从index开始后的所有字符串赋给s
    string s(str,index,len);//将str中从index开始后的len个字符赋给s
    string s(cstr);//将char*类型的字符串赋给s
    string s(chars,len); //将char*类型的前len个字符赋给s
    string s(num,c); //以num个c的字符串作为s的初值
    string s(begin,end);//以区间[begin,end)的字符赋给s
# include <iostream>
# include <string>
using namespace std;
int main()
{
    string str1 = "yesterday once more";
    string str2 ("my heart go on");
    string str3 (str1,6); // = day once more
    string str4 (str1,6,3); // = day

    char ch_music[] = {"Roly-Poly"};

    string str5 = ch_music; // = Roly-Poly
    string str6 (ch_music); // = Roly-Poly
    string str7 (ch_music,4); // = Roly
    string str8 (10,'i'); // = iiiiiiii
    string str9 (ch_music+5, ch_music+9); // = Poly

    str9.~string();

    //cout<<str9<<endl; // 测试输出

    getchar();
    return 0;
}

2、 swap() // 交换两个字符串的内容
用法如下:

# include <iostream>
# include <string>
using namespace std;
int main()
{
    string str1 = "yesterday once more";
    string str2 ("my heart go on");

    str2.swap(str1);
    cout<<str1<<endl; // = my heart go on
    cout<<str2<<endl; // = yesterday once more

    getchar();
    return 0;
}

3、 +=、append()、push_back() 在尾部添加字符
增加字符(这里说的增加是在尾巴上),函数有 +=、append()、push_back()。举例如下:
s+=str;//加个字符串
s+=”my name is jiayp”;//加个C字符串
s+=’a’;//加个字符
s.append(str);
s.append(str,1,3);//不解释了同前面的函数参数assign的解释
s.append(str,2,string::npos)//不解释了
s.append(“my name is jiayp”);
s.append(“nico”,5);
s.append(5,’x’);
s.push_back(‘a’);//这个函数只能增加单个字符

4、insert() //插入字符
在string中间的某个位置插入字符串,可以用insert()函数,这个函数需要指定一个安插位置的索引,被插入的字符串将放在这个索引的后面。
s.insert(0,”my name”);
s.insert(1,str);
这种形式的insert()函数不支持传入单个字符,这时的单个字符必须写成字符串形式。注意:为了插入单个字符,insert()函数提供了两个对插入单个字符操作的重载函数:insert(size_type index,size_type num,chart c)和insert(iterator pos,size_type num,chart c)。其中size_type是无符号整数,iterator是char*,所以,这么调用insert函数是不行的:insert(0,1, ’j’);这时候第一个参数将转换成哪一个呢?所以必须这么写:insert((string::size_type)0,1,’j’)!第二种形式指出了使用迭代器安插字符的形式,在后面会提及。顺便提一下,string有很多操作是使用STL的迭代器的,他也尽量做得和STL靠近。

5、 erase() //删除字符
iterator erase(iterator pos); //删除pos指向的字符,返回指向下一个字符的迭代器
iterator erase(iterator start,iterator end); // 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
basic_string &erase(size_type index=0,size_type num=pos); //删除从index索引开始的num个字符, 返回*this.
参数index 和 num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符

string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "The original string is '" << s << "'" << endl;

s.erase( 50, 14 );
cout << "Now the string is '" << s << "'" << endl;

s.erase( 24 );
cout << "Now the string is '" << s << "'" << endl;

s.erase();
cout << "Now the string is '" << s << "'" << endl;

6、 clear() //删除全部字符
和earse() 一样,都是删除全部字符

string s = "pengwang";
s.clear();
cout<<s<<endl; //s 为空字符串

7、replace() //替换字符
replace 函数的用法非常多,现列举几个比较常用的几个用法
basic_string &replace( size_type index, size_type num, const basic_string &str ); // 用str中的num个字符替换本字符串中的字符,从index开始
basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,size_type num2 ); 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
basic_string &replace( size_type index, size_type num, const char *str ); // 用str中的num个字符(从index开始)替换本字符串中的字符
basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 ); 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
basic_string &replace( size_type index, size_type num1, size_type num2, char ch ); 用num2个ch字符替换本字符串中的字符,从index开始

string s = "They say he carved it himself...from a BIGGER spoon";
string s2 = "find your soul-mate, Homer.";
s.replace( 32, s2.length(), s2 );
cout << s << endl;

8、assign //赋值
basic_string &assign( const basic_string &str ); //用str为字符串赋值
basic_string &assign( const char *str ); //用str为字符串赋值
basic_string &assign( const char *str, size_type num ); // 用str的开始num个字符为字符串赋值,
basic_string &assign( const basic_string &str, size_type index, size_type len ); // 用str的子串为字符串赋值,子串以index索引开始,长度为len
basic_string &assign( size_type num, char ch ); // 用num个字符ch为字符串赋值.

string str1, str2 = "War and Peace";
str1.assign( str2, 4, 3 );
cout << str1 << endl;   //and

9、substr() //截取字符串
substr(int index,int num = npos)
从本字符串中index开始,截取长度为num个字符。如果没有指定num,则将是默认值string::npos,将截取从index后的全部字符

string s("pengwang");
string sub = s.substr(4);
cout << "The substring is " << sub << endl; //wang

10、size() ,length() //获取字符串长度

string s("pengwang");
cout<<s.length()<<endl;
cout<<s.size()<<endl; //8

11、empty() //判断字符串是否为空
bool empty(); 如果字符串为空,返回true,否则返回false,常用语if语句判断

string s ="";
if(s.empty())
  cout<<"s is empty"<<endl;

12、c_str()
和 data()函数一样,返回一个指向正规C字符串的指针, 内容与本字符串相同.
13、at( )
at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告”out of range”错误,并抛出out_of_range异常

string text = "ABCDEF";
char ch = text.at( 2 );  // ch='C'

14、copy()
size_type copy( char *str, size_type num, size_type index );
copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数

string s1;
s1.copy(p, 4, 0);
cout << s1 << endl;
string sub = s.substr(4);

15、capacity() //返回重新分配之前的字符容量
capacity()重新分配内存之前 string所能包含的最大字符数。
16、 查找函数
find()
rfind()
find_first_of()
find_last_of()
find()函数是非常常用的字符串函数,因此应该尤为重视,其语法有如下几种
size_type find( const basic_string &str, size_type index ); //返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
size_type find( const char *str, size_type index ); //返回str在字符串中第一次出现的位置(从index开始查找)。如果没找到则返回string::npos,
size_type find( const char *str, size_type index, size_type length ); //返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
size_type find( char ch, size_type index ); //返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

string str1( "Alpha Beta Gamma Delta" );
unsigned int loc = str1.find( "Omega", 0 );
if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
else
      cout << "Didn't find Omega" << endl;

rfind 函数
size_type rfind( const basic_string &str, size_type index ); //返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
size_type rfind( const char *str, size_type index ); //返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
size_type rfind( const char *str, size_type index, size_type num ); //返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
size_type rfind( char ch, size_type index ); //返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos

int loc;
string s = "My cat's breath smells like cat food.";

loc = s.rfind( "breath", 8 );
cout << "The word breath is at index " << loc << endl;

loc = s.rfind( "breath", 20 );
cout << "The word breath is at index " << loc << endl;

find_first_of() 函数和find类似,
size_type find_first_of( const basic_string &str, size_type index = 0 ); // 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
size_type find_first_of( const char *str, size_type index = 0 ); //查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
size_type find_first_of( const char *str, size_type index, size_type num ); //查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
size_type find_first_of( char ch, size_type index = 0 ); // 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。

find_last_of函数和find_first_of相反,是用于查找字符串中最后一个与某个字符串中某个字符匹配的字符,并返回它的位置,没找到返回string::npos
size_type find_last_of( const basic_string &str, size_type index = npos ); //在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_of( const char *str, size_type index = npos ); //在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
size_type find_last_of( const char *str, size_type index, size_type num ); //在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
size_type find_last_of( char ch, size_type index = npos ); // 在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

# include <iostream>
# include <string>

using namespace std;
int main()
{
    string str = "when i was young, i listen to radio.";
    string::size_type position;

    position  = str.find("listen");

    if (position != str.npos) //npos是个很大的数,如果没找到就会返回npos的值给position
    {
        cout<<"第一次出现的下标是:"<<position<<endl;
    }

    //从字符串下标9开始,查找字符串you,返回you 在str中的下标
    position = str.find("you",9);
    cout<<"str.find("you",9")is:"<<position<<endl;

    //查找子串出现的所有位置
    string substr = "i";
    position = 0;
    int i = 1;
    while((position = str.find_first_of(substr,position)) != string::npos)
    {
        cout<<"position "<<i++<<position<<endl;
        position++;
    }

    //反向查找子串在str中最后出现的位置
    string flag = "to";
    position = str.rfind(flag);
    cout<<"str.rfind(flag):"<<position<<endl;
    getchar();
    return 0;
}

总结了以上这些字符串的用法,能解决我们日常工作中的常用问题了,当然,如果要查看c++字符串的所有功能,则可以访问c++ api官方文档了。http://tool.oschina.net/apidocs/apidoc?api=cpp%2Fen%2Fcpp.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值