String and character manipulation methods in C/s + +

Source: Internet
Author: User
Tags printable characters processing text uppercase letter

This article summarizes the methods of string manipulation in C + +, and is still in the study, not regularly updated.


Input methods for strings

1, a single word can be directly used std::cin, because: std::cin read and ignore the beginning of all white space characters (such as spaces, newline characters, tabs). Reads the character until it encounters a whitespace character again, and the read terminates.

So cin can only read single words . It is obvious that CIN can be used more than once to obtain multiple words;

2, multiple words using the function std::getline(std::cin, s) See the following code:

#include <iostream> #include <string> int main () {std::string line;//Empty Stringwhile (Std::getline (std:: CIN, line)) {
            Read line at time until End-of-file    std::cout << line << Std::endl;//write S to the output}    retur n 0;}

This function accepts two parameters: an input stream object and a string object. The Getline function reads from the next line of the input stream and saves the read content to a newline character. Unlike the input operator, Getline does not omit line breaks at the beginning of a row. Just getline encounters a newline character, even if it is the first one entered, Getline will stop reading and returning. Assuming that the first character is a line break, the string parameter is set to an empty string.

Because the Getline function returns with a newline character discarded, the line break will not be stored in the string object.

prototype:ssize_t getline (char **lineptr, size_t *n, FILE *stream)
This function reads-entire line from stream, storing the text (including the newline and a terminating null character) In a buffer and storing the buffer address in *lineptr.
Before calling Getline, you should place in *lineptr the address of a buffer *n bytes long, allocated with malloc.  If This buffer is a long enough to the line, Getline stores the "line" in this buffer. Otherwise, Getline makes the buffer bigger using realloc, storing the new buffer address back in *lineptr and the Increas Ed size back in *n.
If you set *lineptr to a null pointer, and *n to zero, before the call, then Getline allocates the initial by calling malloc.
In either case, when Getline returns, *lineptr was a char * which points to the text of the line.
When Getline is successful, itReturns the number of characters read(including the newline, but not including the terminating null). This value enables-distinguish null characters that is part of the line from the null character inserted as a ter Minator.
This function was a GNU extension, but it was the recommended-to-read lines from a stream. The alternative standard functions is unreliable.
If An error occurs or end of the file is reached without any bytes read, Getline returns-1. Header files:stdio.h

How to: String

S.empty () Returns True if S is empty; otherwise returns false
If S is an empty string, it returns true, otherwise false is returned.

S.size () Returns number of characters in S
returns the number of characters in S
S[n]returns the character at position N in S; Positions start at 0.
Returns the character of position N in S.position starts from 0 count

Note: 1. When subscript is quoted, it is assumed that exceeding the subscript scope will cause Overflow error. same without error. 2 . The actual data type of the index is type unsigned type string::size_type.

#include <iostream> #include <string>int main () {std::string s = "Hello World";std::cout<<s<< Std::endl;for (Std::string::size_type ix = 0; IX! = S.size (); ++ix) S[ix] = ' * ';    std::cout<< "Now S is:" <<s<<std::endl;    std::cout<< "s ' len is:" <<s.size () << ", s[12]=" <<s[100]<<std::endl;    return 0;}
Note: The loop uses theStd::string::size_type ix = 0;Please use the string built-in type Size_type to operate.becauseThe int type may not be enough string length, so the built-in type size_type (actually can feel isunsigned) is created to ensure the compatibility of each machine,Avoid overflow (and subscript overflow is not a problem)。

A variable that stores the result of a size operation for a string must be of type String::size_type. It is particularly important to assign the return value of size to an int variable.

S1 + s2returns A string equal to the concatenation of S1 and S2
Concatenate S1 and S2 into a new string, returning the newly generated string

"Remarks: can be added continuously, similar to Python.

String s3 = S1 + "," + s2 + "\ n"; .

Note: When a mixed join operation is performed on string and string literals, the left and right operands of the + operator must have at least one string class type of "Imagine that the sub-union knew it was justified." ----1, that is, the + connection must ensure that the first two have a string type! 2, string literals cannot be added directly, string literals and strings are different types, there is no null character in the string ' \ s '. (Updated on 2014.06.24)"

S1 = s2replaces characters in S1 by a copy of S2
Replace the S1 content with a copy of the S2

"Remarks:. It must first release the associated memory occupied by S1, and then allocate it to S2 enough to hold the S2 copy of the memory space, and finally copy all the characters in the S2 to the newly allocated memory space.

V1 = = V2returns true if V1 and v2 are equal; False otherwise
Compare the contents of V1 and V2, and return True if they are equal, otherwise false
! =, <, <=, >=have their normal meanings
Keep these operators in the habit of meaning
Cctype Functions

We often have to deal with a single character in a string object, for example. It is often necessary to know whether a particular character is a blank character, letter, or number. The various character manipulation functions are listed below. The character (or whatever char value) that applies to a String object. These functions are defined in the cctype header file.

Isalnum (c) True if C is a letters or a digit. True if C is a letter or a number.
Isalpha (c) True if C is a letters. True if C is the letter.
Iscntrl (c) True if C is a control character. Assume that C is a controlling character. is True
IsDigit (c) True if C is a digit. If C is a number, true.
Isgraph (c) True if C is not a space and is printable. Assume that C is not a space and can be printed, or true.

Islower (c) True if C is a lowercase letters. True if C is a lowercase letter.
Isprint (c) True if C is a printable character. True if C is a printable character.

"Note: Printable characters refer to characters that can be represented"

Ispunct (c) True if C is a punctuation character. Assume that C is a punctuation mark, true.

Note: punctuation is a printable character other than a number, letter, or (printable) white space character (such as a space).

Isspace (c) True if C is whitespace. Assume that C is a white space character. Is true.

Note: White space characters are spaces, tabs, vertical tabs, carriage returns, line breaks, and any arbitrary type in the paper feed character.

Isupper (c) True if C is a uppercase letter. Suppose c is uppercase. Is true.
Isxdigit (c)

True if c is a hexadecimal digit. If it is a C hexadecimal number, true.

toLower (c)

If C is a uppercase letter, returns its lowercase equivalent; Otherwise returns C unchanged. Assume C capital letters. Returns its lowercase letter, otherwise directly returns C.

toUpper (c)

If c is a lowercase letter, the returns its uppercase equivalent; Otherwise returns C unchanged. If c is a lowercase letter, it returns its uppercase form. Otherwise, return directly to C.

Note: ctype.h is a header file that is defined in the C standard library. Cctype is actually using the C standard library function. the C standard library header file is named name and the C + + version number is named CNAME, with fewer suffixes,. h and a C in front of the header file name indicates that the header file originated from the C standard library. As a result,the contents of theCctype and ctype.h files are the same, only in the form of a more suitable C + + program . in particular, the names defined in the CNAME header file are defined within the namespace STD, and the. h version number does not. usually. a C + + program should take the version number of a header file such as a CNAME, instead of using the name.h version number . the names in the standard library remain consistent in the namespace Std.

The use of the. h version is a burden on the program apes, because they have to remember which standard library names inherit from C and which are specific to C + +. "

String manipulation

The following summary was updated in 2014.10.01, from the classic textbook.

In: S and str are string string,ca is a character array, Str_ca is a string or an array of characters, Str_ca_ch is a string, an array of characters, or a character, CH is a character. N, N1, N2, pos1, pos2 are integers.

Length operation
S.capacity ()

Returns the storage capacity acquired by S;

S.size ()/s.length ()

Returns the length of S;

S.empty ()

It returns true if S does not include a character. otherwise returns false;

S.max_size ()

Returns the maximum possible length of s;

Edit operation
S.append (Str_ca)

Add Str_ca to the end of S and return s;

S.append (CA, N)

Adds the first n characters of the CA to the end of S and returns s;

S.append (n, ch)

n copies of Ch are added to the end of S, returning s;

S.insert (POS, str)

Insert a copy of STR into the POS position of S and return S.

S.insert (POS1, str, POS2, N)

Inserts n characters from the pos2 position in Str into the POS1 position of S. Returns S. "Assume that N is greater than the length of Str. There will be no problem. No overflow error. will only be copied to the end of STR "

S.insert (POS, CA, N)

Insert the first n characters of the CA into the POS position of S, assuming that n is omitted, insert all the characters from the CA into the POS position, and return s;

S.insert (POS, N, CH)

Inserts n copies of the character Ch into the POS position of S. return s;

S.erase (POS, N)

Remove n characters from the Pos starting in S (default pos is 0), return s;

S.replace (POS1, N1, str)

Replace the POS1 position in s with the N1 string with str "Assuming that the N1 is too large, all characters from Pos to S end will be replaced." return s;

S.replace (POS1, N1, CA, N2)

As above, just take the first N2 characters of the CA. return s;

S.swap (str)/swap (S, str)

Exchange the contents of S and str. returns to void.

Copy operation

Supports + + = operators.

S.assign (Str_ca)

Give a copy of Str_ca to S. Returns S.

S.assign (CA, N)

Assigns a string of the first n characters of a CA to S. return s;

S.assign (n, ch)

Assigns a string of n ch to s and returns s;


Returns a copy of the substring of s with n characters starting from POS (default 0) in S.

Find operations
S.find (Str_ca_ch, POS)

Returns the position of the first in s greater than or equal to POS, and the next character in this position starts with the character in S and matches the corresponding character in Str_ca_ch. Assuming that there is no such position, the NPOs is returned. Pos Default Feel 0

S.find_first_of (Str_ca_ch, POS)

Returns the position of the characters in s that are greater than or equal to the random characters in the first and str_ca_ch of the POS, assuming that no such position returns the default value of Npos,pos of 0

S.find_first_not_of (Str_ca_ch, POS)

Returns the position of characters in s that are greater than or equal to the random characters in the first and str_ca_ch of the POS, assuming that no such position returns NPOs. The default value for POS is 0

S.find_last_of (Str_ca_ch, POS)

Returns the position of a character in s that is less than or equal to the maximum number of points in the Pos and the random character in the str_ca_ch, assuming no such position returns the default value of Npos,pos is 0

S.find_last_not (Str_ca_ch, POS)

Returns the position of a character in s that is less than or equal to the maximum number of points in the POS and str_ca_ch characters that do not match, assuming that no such position returns the default value of Npos,pos is 0

S.rfind (Str_ca_ch, POS)

Returns the last position in s that is less than or equal to POS, and str_ca_ch.size () characters starting at this position match the corresponding characters in the str_ca_ch. Assuming that there is no such position, the NPOs is returned. Pos Default Feel NPOs

Compare operations

Supports the above <, <=, >, >=, = =,! = operators. (Str_ca)

The return value is positive, 0, negative

Conversion of string and C-style strings s.c_str ()

Returns a constant character array. This array consists of characters stored in S. Ends with a null character; ()

Returns a constant character array. This array consists of characters stored in S, but not ending with a null character.

S.copy (Chararray, POS, N)

Replace Chararray with n characters from the Pos starting in S, assuming that the POS is omitted, starting from 0, assuming that N is too large. Then copy the character until S ends, returning the number of characters that are finally copied.

Note:both data () and C_STR () can be used to extract the character array required by the open operation from a file name, which may be exposed in a possible file operation.

Sample Example"This subsection was updated in 2014.06.24"

The original title is also a blog post:container vector usage

#include <iostream> #include <string> #include <vector>using std::cin; Using Std::cout; Using Std::endl; Using Std::string; Using std::vector;string Deal_word (string word) {//uses the C++11 Auto statement and the range for statement for (auto &c:word) {if (not ispunct (c ) {c = ToUpper (c);//Connect non-punctuation characters to String}else{word.erase (Word.size ()-1, 1);//can only delete the last punctuation mark.

There are limitations!

}}return Word;} String Deal_word2 (string word) {//uses subscript and c++11 Decltype for (Decltype (Word.size ()) index = 0; Index! = word.size (); ++ind Ex) {if (not ispunct (Word[index])) {Word[index] = ToUpper (Word[index]);} else {word.erase (index, 1);//delete a character at the specified position. This is the punctuation index-= 1; Make sure the subscript doesn't cross!


}} return word;} int main () {string Word;//cache input Word vector<string> text;//empty vectorcout<< "Please input the text:" <<e Ndl Prompt input while (std::cin >> word and word! = "inputover")//Inputover is used to mark the end of input, or CTRL + Z to stop typing {word = Deal_wor D (Word); word processing text.push_back (word); Append Word to Text}for (Std::vector<int>::size_type ix =0, j = 0; IX! = Text.size (); ++ix, ++j) {if (j==8)//8 words Line {cout<<endl;//newline j = 0;//Again Count} cout<<text[ix]<< ""; Add a space. } return 0;}

Two methods of processing words have been rewritten.

New features in c++11 are used!

"updated in 2014.06.05, the problem of not adding or removing content from iterators in a for loop has been overlooked. This really does not merit rationale, or choose to copy it (that is, the original method)!

Error probability is low, readability is strong. The efficiency has yet to be verified.

I want to add ... 】


Str.Erase () method: from Baidu
1, Erase (POS, n); Deletes the n characters starting from the Pos. For example, erase (0,1) deletes the first character
2, erase (position); Delete a character at position (position is a string type iterator)
3, Erase (first, last); Delete characters from first to last (both first and last are iterators)

This article by @the_third_wave (blog address: original.

Not regularly updated. Please correct me for any errors.

If you see this blog post as incomplete, that's why I first announced half of the crawler to prevent it. Please see the original author blog.

Let's say this blog post is helpful to you for a good network environment. not recommended reprint, recommended collection!

If you must reprint, please bring the suffix and the address of this article.

String and character manipulation methods in C/s + +

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: 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.