- String 类是模板类
typedef basic_string<char> string ;
- 使用String类要包含头文件
- String 对象的初始化
string str1( "hello" );
string month = "February" ;
string str2( 9 , 'x' );
- String 类错误的初始化方法
string error1 = 'c'; //错误
string error2( 'u' ); //错误
string error3 = 44 ; //错误
string error4( 8 ); //错误
- 可以将字符赋值给string对象
string s ;
s = 'a' ; //正确的赋值,但初始化时这样写就会发生错误
- 程序示例
string s1( "hello world" );
cout << s1 << endl;
string s2( 9 , 'x' );
cout << s2 << endl;
string month = "February";
cout << month << endl;
string s3;
s3 = 'a';
cout << s3 << endl;
结果如下:
- string 对象的长度用成员函数length()提取
string s( "hello" );
cout << s.length();
- string 支持流读取运算符
string stringObject;
cin >> string0bject;
- string 支持getline()函数
string s;
getline( cin , s );
- 用 “=” 赋值
string s1( "cat" ) ,
s2;
s2 = s1;
- 用assign复制
string s1( "cat" ) ,
s3;
s3.assign( s1 );
- 用assign成员函数部分复制
string s1( "catdog" ) ,
s3;
s3.assign( s1 , 1 , 3 );
//从s1下标为1的字符开始复制三个字符
- 单个字符赋值
s2[5] = s1[3] = 'a';
- 逐个访问string对象中的字符
string s1( "hello" );
for( int i = 0 ; i < s1.length() ; i++ )
{
cout << s1.at( i ) << endl;
}
//其中成员函数at( int index )会做范围检查,如果越界,会
//产生out_of_range异常,而下标运算符[]不会做范围检查
- 用 “+” 运算符连接字符串
string s1( "good" ) ,
s2( "morning" );
s1 += s2;
cout << s1 << endl;
- 用成员函数append连接字符串
string s1( "good" ),
s2( "morning" );
s1.append( s2 );
cout << s1 << endl;
s2.append( s1 , 3 , s1.size() );
cout << s2 << endl;
//从下标3开始,s1.size()个字符,如果字符串内没有足够字符,则复制到字符串的结尾
- 用关系运算符比较字符串的大小
== > >= < <= !=
返回值都是bool类型,成功返回true,失败返回false
例如:
string s1("hello"),
s2("hello"),
s3("hell");
bool b = ( s1 == s2 );
cout << b << endl;
b = ( s1 == s3 );
cout << b << endl;
b = ( s1 > s3 );
cout << b << endl;
程序的输出结果如下:
- 用成员函数compare比较string的大小
cpp
string s1("hello"),
s2("hello"),
s3("hell");
int f1 = s1.compare( s2 );
int f2 = s1.compare( s3 );
int f3 = s3.compare( s1 );
int f4 = s1.compare( 1 , 2 , s3 , 0 , 3 ); //s1 1-2 s3 0-3
int f5 = s1.compare( 0 , s1.size() , s3 ); //s1 , s3 , 0-end
cout << f1 << endl << f2 << endl ;
cout << f3 << endl << f4 << f5 ;
结果如下:
- 子串成员函数substr
string s1( "hello world" ),
s2 = s1.substr( 4 , 5 );
cout << s2 << endl;
结果如下图:
- 交换 string 成员函数 swap
string s1( "hello world" ),
s2( "really" );
s1.swap( s2 );
cout << s1 << endl;
cout << s2 << endl;
寻找string中的字符
- 成员函数find
string s1( "hello world" );
s1.find( "lo" );
/*
在s1中从前向后查找,“lo” 第一次出现的地方,如果找到,
返回"lo"开始的位置,即l所在 位置的下标。如果找不到,返回
string::npos( string定义的静态常量 )。
*/
成员函数rfind()
string s1( "hello world" );
s1.rfind( "lo" );
/*
在s1中从后向前查找,"lo"第一次出现的地方,如果找到,返回"lo"
开始的位置,即l所在位置的下标。如果找不到,返回string::npos
*/
以上两段代码运行结果如下,其中最后一个为string::npos的值
查到了返回值为子串的首字母在串中的索引位置,而查不到,则返回值为string::npos,这是一个longlong类型的返回值,如果使用int类型的变量去接收返回值,则会得到-1(dev c++ 编译器运行的结果);
从某个指定的索引的位置开始查找某个字符串
string s1( "hello world" );
cout << s1.find( "ll" , 1 ) << endl;
cout << s1.find( "ll" , 2 ) << endl;
cout << s1.find( "ll" , 3 ) << endl;
//分别从s1中的下标1,2,3查找"ll"字符串
- 寻找string中的字符
成员函数find_first_of
string s1( "hello world" );
s1.find_first_of( "abcd" );
/*
在s1中从前向后查找,"abcd"中任何一个字符第一次出现的地方,如果
如果找到,返回找到字母的位置,如果找不到,返回string::npos。
*/
成员函数find_last_of
string s1( "hello world" );
s1.find_last_of( "abcd" );
/*
在s1中查找"abcd"中任何一个字符最后一次出现的地方,如果找到
返回找到字母的位置,如果找不到,返回string::npos。
*/
成员函数find_first_not_of
string s1( "hello world" );
s1.find_first_not_of( "abcd" );
/*
在s1中查找不在"abcd"中的字母第一次出现的地方,如果找到,返回
字母的位置,如果找不到,返回string::npos
*/
成员函数find_last_not_of
string s1( "hello world" );
s1.find_last_not_of( "abcd" );
/*
从后往前找s1中不在"abcd"中的字母第一次出现的地方,如果找到
返回字母的位置,如果找不到,返回string::npos。
*/
示例代码:
string s1( "hello worlld" );
cout << s1.find( "ll" ); // 2
cout << s1.find( "abc" ); //4294967295
cout << s1.rfind( "ll" ); // 9
cout << s1.rfind( "abc" ); // 4294967295
cout << s1.find_first_of( "abcde" ) << endl; // 1
cout << s1.find_first_of( "abc" ) << endl; // 4294967295
cout << s1.find_last_of( "abcde" ) << endl; // 11
cout << s1.find_last_of( "abc" ) << endl; // 4294967295
cout << s1.find_first_not_of( "abcde" ) << endl; // 0
cout << s1.find_first_not_of( "hello world" ) << endl; //4294967295
cout << s1.find_last_not_of( "abcde" ) << endl; // 10
cout << s1.find_last_not_of( "hello world" ) << endl; // 4294967295
结果如下图所示:
- 删除string中的字符
成员函数erase()
string s1( "hello world" );
s1.erase( 5 );
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.length() << endl;
//去掉下标5及其之后的字符
输出结果如下:
- 替换string中的字符,成员函数replace()
string s1( "hello world" );
s1.replace( 2 , 3 , "haha" );
//将s1中下标2开始的3个字符换成"haha"
cout << s1 << endl;
运行结果:
替换函数replace的另一种用法
string s1( "hello world" );
s1.replace( 2 , 3 , "haha" , 1 , 2 );
cout << s1 << endl;
//将s1中下标2开始的3个字符替换为"haha"中下标1开始的两个字符
结果:
- 在string中插入字符,成员函数insert()
string s1( "hello world" );
string s2( "show insert" );
s1.insert( 5 , s2 );
cout << s1 << endl;
s1.insert( 2 , s2 , 5 , 3 );
//将s2中下标5开始的3个字符插入s1下标2的位置
结果如下:
- 转化成c语言式的char *字符串
- 成员函数data()
string s1( "hello world" );
const char *p1 = s1.data();
for( int i = 0 ; i < s1.length() ; i++ )
{
printf( "%c" , *( p + i ) );
}
//s1.ddata()返回一个char *类型的字符串,对s1的修改可能会使p1出错
成员函数c_str()
string s1( "hello world" );
printf( "%s\n" , s1.c_str() );
//c_str返回传统的const char *类型字符串,且该字符串以'\0'结尾
结果:
- 字符串的流处理
除了标准流和文件流的输入输出外,还可以从string进行输入输出。类似istream和ostream进行标准输入输出,也可以从istringstream和ostringstream进行字符串上的输入输出,也称为内存的输入输出。
#include <string>
#include <iostream>
#include <sstream>
string input( "input test 123 4.7 A" );
istringstream inputString( input) ;
string string1 ,
string2 ;
int i;
double d;
char c;
inputString >> string1 >> string2 >> i >> d >> c ;
cout << string1 << endl << string2 << endl;
cout << i << endl;
cout << d << endl;
cout << c << endl;
long L;
if( inputString >> L )
{
cout << "long" << endl;
}
else
{
cout << "empty" << endl;
}
ostringstream outputString ;
int a = 10 ;
outputString << "This" << a << "ok" << endl;
cout << outputString.str();
结果: