string类

[+]
  1. 查找(find)
  2. substr
  3. find_first_of

1、const char *data();

data()函数返回指向自己的第一个字符的指针.

 

 

[cpp] view plain copy print ?
  1. string str3="you are stupid!";  
  2.     char* cc=(char*)str3.data();  
  3.     *(cc+1)='n';  
  4.     cout<<cc<<endl;  
<textarea style="DISPLAY: none" class="cpp" rows="5" cols="44" name="code">string str3=&quot;you are stupid!&quot;; char* cc=(char*)str3.data(); *(cc+1)='n'; cout&lt;&lt;cc&lt;&lt;endl;</textarea>

 

由于data()函数返回的是const char*,所以不能直接通过指针修改返回的字符串的值,如果要修改,必须把类型转化为char*.

 

2、const char *c_str();

c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同.

 

作用类似于data(),返回的也是指向常量字符串的指针。

 

3、size_type copy( char *str, size_type num, size_type index );

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

 

非常有用的函数,要注意的是参数str一般为数组的名称,不能使用指针类型参数,因为需要预分配空间的。

 

[cpp] view plain copy print ?
  1. char dd[3];  
  2.     str3.copy(dd,3,0);  
  3.     cout<<dd<<endl;  
<textarea style="DISPLAY: none" class="cpp" rows="5" cols="42" name="code">char dd[3]; str3.copy(dd,3,0); cout&lt;&lt;dd&lt;&lt;endl;</textarea>

 

4、int compare( const basic_string &str );
  int compare( const char *str );
  int compare( size_type index, size_type length, const basic_string &str );
  int compare( size_type index, size_type length, const basic_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

[cpp] view plain copy print ?
  1. string str1="hello world!",str2="hello hell!";  
  2.     cout<<( (str1.compare(str2)== 0)? "equal""not equal" )<<endl;  
  3.     cout<<( (str1.compare(0,5,str2,0,5)== 0)? "equal""not equal" )<<endl;  
  4.     const char* str3="hellu sir!";  
  5.     cout<<( (str1.compare(0,5,str3,0,5)== 0)? "equal""not equal" )<<endl;  
<textarea style="DISPLAY: none" class="cpp" rows="10" cols="47" name="code">string str1=&quot;hello world!&quot;,str2=&quot;hello hell!&quot;; cout&lt;&lt;( (str1.compare(str2)== 0)? &quot;equal&quot;: &quot;not equal&quot; )&lt;&lt;endl; cout&lt;&lt;( (str1.compare(0,5,str2,0,5)== 0)? &quot;equal&quot;: &quot;not equal&quot; )&lt;&lt;endl; const char* str3=&quot;hellu sir!&quot;; cout&lt;&lt;( (str1.compare(0,5,str3,0,5)== 0)? &quot;equal&quot;: &quot;not equal&quot; )&lt;&lt;endl;</textarea>

 

5、

查找(find)

语法:

  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 );

find()函数:

  • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

[cpp] view plain copy print ?
  1. string str1( "Alpha Beta Gamma Delta" );  
  2.    unsigned int loc = str1.find( "Omega", 0 );  
  3.    if( loc != string::npos )  
  4.      cout << "Found Omega at " << loc << endl;  
  5.    else  
  6.      cout << "Didn't find Omega" << endl;  
<textarea style="DISPLAY: none" class="cpp" rows="8" cols="45" name="code"> string str1( &quot;Alpha Beta Gamma Delta&quot; ); unsigned int loc = str1.find( &quot;Omega&quot;, 0 ); if( loc != string::npos ) cout &lt;&lt; &quot;Found Omega at &quot; &lt;&lt; loc &lt;&lt; endl; else cout &lt;&lt; &quot;Didn't find Omega&quot; &lt;&lt; endl; </textarea>

 

6、

substr

语法:

  basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

 

[cpp] view plain copy print ?
  1. string s("What we have here is a failure to communicate");  
  2.   
  3.    string sub = s.substr(21);  
  4.   
  5.    cout << "The original string is " << s << endl;  
  6.    cout << "The substring is " << sub << endl;  
<textarea style="DISPLAY: none" class="cpp" rows="11" cols="48" name="code"> string s(&quot;What we have here is a failure to communicate&quot;); string sub = s.substr(21); cout &lt;&lt; &quot;The original string is &quot; &lt;&lt; s &lt;&lt; endl; cout &lt;&lt; &quot;The substring is &quot; &lt;&lt; sub &lt;&lt; endl; </textarea>

 

7、

find_first_of

语法:

  size_type find_first_of( const basic_string &str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index = 0 );
  size_type find_first_of( const char *str, size_type index, size_type num );
  size_type find_first_of( char ch, size_type index = 0 );

find_first_of()函数:

  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
  • 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。

 

[cpp] view plain copy print ?
  1. string str1( "Alpha Beta Gamma Delta" );  
  2.     cout<<str1.find_first_of("B",7)<<endl;  
  3.     cout<<string::npos<<endl;  

你可能感兴趣的:(string类)