打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++ Strings
userphoto

2011.03.08

关注
Constructors
Syntax:
 string( size_type length, char ch );
 string( const char *str );
 string( input_iterator start, input_iterator end );
append
Syntax:
 basic_string &append( const basic_string &str );
 basic_string &append( const char *str );
 basic_string &append( const basic_string &str, size_type index, size_type len );
 basic_string &append( const char *str, size_type num );
 
size
Syntax:
 size_type size();
The size() function returns the number of characters currently in the string.
at
Syntax:
 reference at( size_type index );
The at() function returns a reference to the character at location index. If index is not within the string, then at() reports an out of range error by throwing an object of the out_of_range class. For example, this code:
    string text = "ABCDEF";
    char ch = text.at( 2 );
displays the character 'C'.
begin
Syntax:
 iterator begin();
The begin() function returns an iterator to the first element of the current string.
erase
Syntax:
 iterator erase( iterator pos );
 iterator erase( iterator start, iterator end );
 basic_string &erase( size_type index = 0, size_type num = npos );
 
The erase() function either:
·                         removes the character pointed to by pos, returning an iterator to the next character,
·                         removes the characters between start and end, returning an iterator to the character after the last character removed,
·                         or removes num characters from the current string, starting at index, and returns *this.
The parameters index and num have default values, which means that erase() can be called with just index to erase all characters after index or with no arguments to erase all characters. For example:
    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;
will display
The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
Now the string is 'So, you like donuts, eh? Well, have all the donuts'
Now the string is 'So, you like donuts, eh?'
Now the string is ''
find
Syntax:
 size_type find( const basic_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 );
The function find() either:
returns the first occurrence of str within the current string, starting at index, string::npos if nothing is found, 
returns the first occurrence of str within the current string and within length characters, starting at index, string::npos if nothing is found, 
or returns the index of the first occurrence ch within the current string, starting at index, string::npos if nothing is found. 
For example, 
    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;
insert
Syntax:
 iterator insert( iterator i, const char &ch );
 basic_string &insert( size_type index, const basic_string &str );
 basic_string &insert( size_type index, const char *str );
 basic_string &insert( size_type index, const char *str, size_type num );
 basic_string &insert( size_type index, size_type num, char ch );
 void insert( iterator i, size_type num, const char &ch );
 void insert( iterator i, iterator start, iterator end );
The very multi-purpose insert() function either:
inserts ch before the character denoted by i, 
inserts str into the current string, at location index, 
inserts a substring of str (starting at index2 and num characters long) into the current string, at location index1, 
inserts num characters of str into the current string, at location index, 
inserts num copies of ch into the current string, at location index, 
inserts num copies of ch into the current string, before the character denoted by i, 
or inserts the characters denoted by start and end into the current string, before the character specified by i. 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
c++ substr()和assign()函数用法
转: std::string用法详解
java中判断字符串是否是一个整数(转载)
char[] && string
char * ,char,string与NSString转化(objec
C/C++字符串查找函数 <转>
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服