string
-----内容摘自c++API文档,内容可能较为老旧-----
特性
- char * 是一个指针,string是一个类:string封装了char *,管理这个字符串,是一个char * 型的容器
- string封装了许多的成员方法:查找find、拷贝copy、删除delete、替换replace、插入insert、添加append
- 不用考虑内存释放和越界
- string和char * 可以相互转化,通过string提供的 c_str() 来完成
string常用API
操作符
可以用 ==, >, <, >=, <=, and !=比较字符串. 可以用 + 或者 += 操作符连接两个字符串, 并且可以用[]获取特定的字符
添加文本(append)
string &append( const string &str );
string &append( const char *str );
string &append( const string &str, size_type index, size_type len );
string &append( const char *str, size_type num );
string &append( size_type num, char ch );
string &append( input_iterator start, input_iterator end );
append() 函数可以完成以下工作:
- 在字符串的末尾添加str,
- 在字符串的末尾添加str的子串,子串以index索引开始,长度为len
- 在字符串的末尾添加str中的num个字符,
- 在字符串的末尾添加num个字符ch,
- 在字符串的末尾添加以迭代器start和end表示的字符序列.
string str = "Hello World";
str.append( 10, '!' );
cout << str << endl;
Hello World!!!!!!!!!!
赋值(assign)
string &assign( const string &str );
string &assign( const char *str );
string &assign( const char *str, size_type num );
string &assign( const string &str, size_type index, size_type len );
string &assign( size_type num, char ch );
函数以下列方式赋值:
- 用str为字符串赋值,
- 用str的开始num个字符为字符串赋值,
- 用str的子串为字符串赋值,子串以index索引开始,长度为len
- 用num个字符ch为字符串赋值.
string str1, str2 = "War and Peace";
str1.assign( str2, 4, 3 );
cout << str1 << endl;
and
at
reference at( size_type index );
at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常。 比如下列代码:
string text = "ABCDEF";
char ch = text.at( 2 );
'C'
c_str
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.
容量(capacity)
size_type capacity();
capacity()函数返回在重新申请更多的空间前字符串可以容纳的字符数. 这个数字至少与 size()一样大.
比较(compare)
语法:
int compare( const string &str );
int compare( const char *str );
int compare( size_type index, size_type length, const string &str );
int compare( size_type index, size_type length, const string &str,
size_type index2, size_type length2 );
int compare( size_type index, size_type length, const char *str, size_type length2 );
compare()函数以多种方式比较本字符串和str,返回:
返回值 | 情况 |
---|---|
小于零 | this < str |
零 | this == str |
大于零 | this > str |
不同的函数:
- 比较自己和str,
- 比较自己的子串和str,子串以index索引开始,长度为length
- 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
- 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length
拷贝(copy)
size_type copy( char *str, size_type num, size_type index );
copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数
查找(find)
size_type find( const string &str, size_type index );
size_type find( const char *str, size_type index );
size_type find( const char *str, size_type index, size_type length );
size_type find( char ch, size_type index );
find()函数:
- 返回str在字符串中第一次出现的位置(从index开始查找)*。*如果没找到则返回string::npos,
- 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
- 返回字符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 string &str, size_type index );
size_type rfind( const char *str, size_type index );
size_type rfind( const char *str, size_type index, size_type num );
size_type rfind( char ch, size_type index );
rfind()函数:
- 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
- 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
- 返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos
例如,在下列代码中第一次调用rfind()返回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;
交换(swap)
void swap( basic_string &str );
swap()函数把str和本字符串交换。例如:
string first( "This comes first" );
string second( "And this is second" );
first.swap( second );
cout << first << endl;
cout << second << edl;
And this is second
This comes first
替换(replace)
string &replace( size_type index, size_type num, const string &str );
string &replace( size_type index1, size_type num1, const string &str,
size_type index2, size_type num2 );
string &replace( size_type index, size_type num, const char *str );
string &replace( size_type index, size_type num1, const char *str, size_type num2 );
string &replace( size_type index, size_type num1, size_type num2, char ch );
string &replace( iterator start, iterator end, const string &str );
string &replace( iterator start, iterator end, const char *str );
string &replace( iterator start, iterator end, const char *str, size_type num );
string &replace( iterator start, iterator end, size_type num, char ch );
replace()函数:
- 用str中的num个字符替换本字符串中的字符,从index开始
- 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
- 用str中的num个字符(从index开始)替换本字符串中的字符
- 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
- 用num2个ch字符替换本字符串中的字符,从index开始
- 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
- 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
- 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.
例如,以下代码显示字符串"They say he carved it himself…find your soul-mate, Homer."
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;