String. find () function usage

Source: Internet
Author: User

1. The position of string s1 in s is returned. If no position is found,-1 is returned.

# Include
 
  
# Include
  
   
Using namespace std; int main () {string s = "what are you dong"; string s1 = "are"; int position; position = s. find (s1); if (position =-1) cout <"not find" <
   
    
# Include
     
      
# Include
      
       
Using namespace std; int main () {string s = "hahahahaha"; string s1 = "a"; int position; position = s. find_first_of (s1); cout <
       <
        

3. Search for string s1 starting from string s subscript a and return the starting position s. find (s1, a). If no result is found, return-1. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> # include # Include Using namespace std; int main () {string s = "have a good time"; string s1 = "good"; int position; position = s. find (s1, 3); cout < <<

4. Search for all start positions of the character s1 in s

# Include
         
          
# Include
          
           
Using namespace std; int main () {string s = "abb"; string s1 = "a"; // char s1 = 'a'; int position = 0; int I = 0; while (position = s. find_first_of (s1, position ))! = String: npos) {cout <
           <
            
# Include
            
             
# Include
             
              
Using namespace std; int main () {string s = "aaabcd"; string s1 = "aaafcd"; int position; position = s. find_first_not_of (s1); cout <
              <
               


6. reverse search: returns the position of s1 in s.

# Include
                
                 
# Include
                 
                  
Using namespace std; int main () {string s = "aaabcd"; string s1 = "abc"; int position; position = s. rfind (s1); cout <
                  <
                   


References:

Http://www.jb51.net/article/37560.htm


<
< <
<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.