C + +--string operations

Source: Internet
Author: User

sequential Container--extra string manipulation

In addition to the operations that are common to sequential containers, thestring type provides some additional operations. These operations can be broadly divided into two parts:

1. Provide the conversion between the string class and the C-style character rental;

2. Added a version that allows us to use subscripts instead of iterators .

other ways to construct string
Other ways to construct string
N,len2 and Pos2 are unsigned values
string S (cp,n) S is a copy of the first n characters in the array that the CP points to. This array should contain at least n characters
string S (s2,pos2) S is a copy of the string S2 characters starting from subscript Pos2. If Pos2>s2.size (), the behavior of the constructor is undefined.
string S (s2,pos2,len2)

S is a copy of string S2 len2 characters starting from subscript Pos2. If Pos2 > S2.size (), the behavior of the constructor is undefined. No matter what the value of Len2 is,

The constructor has a maximum copy of S2.size ()-pos2 characters.

These constructors accept a string or a const char* parameter and also accept (optionally) a parameter that specifies how many characters to copy (POS2,LEN2).

substr (pos,n) Operation

The substr operation returns a string that is a copy of the entire shipment of the original string. Can be passed to substr an optional start position and count value:

string s ("Hello World");

  // returns a string containing a copy of n characters from the Pos beginning in S. The default value for POS is 0. The default value for n is s,size ()-pos, which is the copy of all characters starting from Pos

  String S1=s.substr (Pos,n);

other ways to change string

Modifying the operation of a string Common scenarios
S.assign (args) Replaces the character in s with the character specified by args. Returns a reference to S Replace All of the contents in S
S.append (args) Appends args to the end of S. Returns a reference to S. appends a new character to the end of S
S.insert (Pos,args)

Inserts the specified args character before the Pos. POS can make a subscript or an iterator.

The version that accepts the subscript returns a reference to S, and the version that accepts the iterator returns an iterator that points to the first inserted character.

at a given location before Insert new character
S.replace (Range,args)

Removes the character from range in S and replaces the character specified by args.

Range is either a subscript and a length, or a pair of iterators pointing to S. Returns a reference to S.

deletes the specified range character and inserts a new character

(The old and new characters may not be the same length)

the new characters can come from: 1. Another string;2. A character pointer (a pointer to an array of characters);3. A list of characters enclosed by a curly brace. When a character comes from a string or a character pointer, we can pass an extra parameter to control whether it is a copy or all of the characters.    For example, S.insert (0, S2, 0, S2.size ()); Insert S2.size () characters starting with s2[0] in S2 before s[0].

Not every function supports all forms of arguments. For example, insert does not support subscript and initialize list parameters. Similarly, if we want to specify the insertion point with an iterator, we cannot specify the origin of the new character with the character pointer.

String search Operation

The string class provides 6 different search functions, each of which has 4 overloaded versions. Each search operation returns a String::size_type (unsigned type) value that represents the subscript where the match occurred. If not found, returns NPOs (initialized to-1).

  

String search operation
S.find (args) Find where args first appeared in S
S.rfind (args) Find where args last appeared in S
S.find_first_of (args) Finds the position of the first occurrence of any character in the args in S
S.find_last_of (args) Finds the last occurrence of any character in the args in S
S.find_first_not_of (args) Find the first character in s that is not in args
S.find_last_not_of (args) Find the last character in S that is not in args

args must be one of the following forms

note: The 2nd parameter does not represent a position, unlike the member function that previously changed string
Span style= "FONT-SIZE:18PX;" >c, pos find characters from position Pos in S, c. Pos defaults to 0
S2, pos

CP, pos

from position Pos in S, look for the C-style string that the pointer cp points to at the end of a null character.

pos defaults to 0

CP, POS, n

pos and n no default

Compare function

The standard library string type provides a set of compare functions, similar to the strcmp function of the C standard library, that s.compare (args) returns 0, positive, or negative depending on whether s is equal to, greater than, or less than the string specified by the parameter.

Depending on the parameters, the Compare function has 6 overloaded versions. specific args form see C + + primer P327, table 9.15.

Numeric Conversions

The new standard introduces several functions that enable the conversion of numeric data to the standard library string :

For specific conversion functions, see C++primer P328, table 9.16.

C + +--string operations

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.