Functions commonly used in string

Source: Internet
Author: User

Functions commonly used in string

Found in the string in the processing of this string is very useful, just found an article put here.

Use string instead of char * array, sort using sort sort algorithm, and use the unique function to go heavy
1, Define
string S1 = "Hello";
String s2 = "World";
String s3 = S1 + "," + s2 + "!\n";
2, append
S1 + = ", shanshan\n";
3, Compare
if (S1 = = s2)
.....
else if (S1 = = "Hello")
.....
4, string overloaded many operators, including +, + =, <,, =,  , [], <<, >>, etc., formal these operators, the string operation is very convenient

#include<string>
#include<iostream>
using namespaceStd
intMain () {
String Strinfo= "Please input your name:";
cout << Strinfo;
Cin >> Strinfo;
if(Strinfo = = "Winter" )
cout << "You are winter!"<<endl;
Else if(Strinfo! = "Wende" )
cout << "You is not wende!"<<endl;
Else if(Strinfo < "Winter")
cout << "your name should be ahead of winter"<<endl;
Else
cout << "your name should is after of winter"<<endl;
Strinfo + = ", Welcome to china!";
cout << strinfo<<endl;
cout << "Your name is:"<<endl;
String strtmp = "How is it ?"+ Strinfo;
for(inti = 0; I < strtmp.size (); i + +)
cout<<strtmp[i];
return0;
}
5. Find function
Because lookups are one of the most frequently used features, String provides a very rich look-up function. The list is as follows:
function name description
find find
rfind reverse lookup
find_fi rst_of
find_first_not_of find any character that does not contain a substring, return to the first position
find_last_of find any characters that contain substrings, return to the last position
find_last_not_of find any characters that do not contain substrings, return to the last position
The above functions are overloaded 4 times, the following is the FIND_FIRST_OF function as an example of their arguments, the other functions and their parameters, that is, a total of 24 functions:
Size_type find_first_of (Constbasic_string& s, size_type pos = 0)
Size_type find_first_of (Constchart* s, size_type pos, Size_type N)
Size_type find_first_of (Constchart* s, size_type pos = 0)
Size_type find_first_of (CharT c, size_type pos = 0)

All lookup functions return a size_type type, which is typically the location of the string found and returns String::npos if not found.
In fact, String::npos represents-1. Return-1 if not found. Examples are as follows:
#include<string>
#include<iostream>
using namespaceStd
intMain () {
String Strinfo= "//*---Hello Word!......------";
String Strset= "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
intFirst = strinfo.find_first_of (Strset);
if(first = = String::npos) {
cout<< "Not find any characters"<<endl;
return-1;
}
intLast = strinfo.find_last_of (Strset);
if(last = = String::npos) {
cout<< "Not find any characters"<<endl;
return-1;
}
cout << strinfo.substr (First, Last-first + 1) <<endl;//string.substr is a substring
return0;
}
6. Insert function, replace function and erase function

The string simply provides the Replace function by location and interval, and cannot replace another string in the specified string with one.
Example:
#include<string>
#include<iostream>
using namespaceStd
intMain () {
String Strinfo= "This is Winter, Winter is a programmer. Do you know Winter?";
cout<< "orign string is: \ n"<<strinfo<<endl;
String_replace (Strinfo, "Winter", "Wende");
cout<< "After replace Winter with Wende, the string is: \ n"<<strinfo<<endl;
return0;
}

String.erase (Pos,srclen);//srclen is the length of the deletion
String.Insert (POS,STRDST); POS is positioned, STRDST is the inserted function
voidString_replace (String & Strbig,ConstString & Strsrc,ConstString &strdst) {
String::size_type pos=0;
String::size_type srclen=strsrc.size ();
String::size_type dstlen=strdst.size ();
while((Pos=strbig.find (STRSRC, pos))! = String::npos) {
Strbig.erase (POS, Srclen);
Strbig.insert (POS, STRDST);
pos + = Dstlen;
}
}

Functions commonly used in string

Related Article

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.