Home > Backend Development > C++ > body text

String processing techniques in C++

WBOY
Release: 2023-08-22 16:34:46
Original
1536 people have browsed it

String processing techniques in C++

In C, string (string) is a very common data type. Moreover, string processing is also a very important link in programming. This article will introduce some commonly used string processing techniques in C, hoping to be helpful to readers.

1. String class in C

In C, string is a class, included in the header file . There are many member functions in the string class. Here are some commonly used member functions:

1. append() function: used to add one string to the end of another string.

For example:

string str1 = "Hello";

string str2 = "World";

str1.append(str2);

cout

2. length() function: used to obtain the length of a string.

For example:

string str = "Hello World";

int len ​​= str.length(); //The value stored in the len variable is 11

3. substr() function: used to intercept part of a string.

For example:

string str = "Hello World";

string s = str.substr(6, 5); //From the 6th character of the string Start, intercept the substring of length 5

cout

4, erase() function: used to delete strings part.

For example:

string str = "Hello World";

str.erase(5, 6); //Delete the length starting from the 5th character in the string It is a substring of 6

cout

2. String traversal

1. Use subscripts Access:

Use subscripts to access each element of the string.

For example:

string str = "Hello World";

for (int i = 0; i

cout << str[i] << " ";
Copy after login

}

The output result is:

H e l l o W o r l d

2. Use iterator:

Use iterator in C ( iterator), which can easily traverse strings.

For example:

string str = "Hello World";

for (string::iterator it = str.begin(); it != str.end() ; it) {

cout << *it << " ";
Copy after login

}

The output result is:

H e l l o W o r l d

3. Splitting and merging strings

1. String splitting:

You can use objects of the stringstream class to split the string according to a certain delimiter.

For example:

string str = "Hello World, This is a good day!";

stringstream ss(str);

string s;

while (getline(ss, s, ',')) {

cout << s << endl;
Copy after login

}

The output result is:

Hello World

This is a good day!

2. String merging:

You can use objects of the stringstream class to merge multiple strings.

For example:

stringstream ss;

string s1 = "Hello";

string s2 = "World!";

ss

string s = ss.str();

cout

The output result is:

Hello World!

IV. Other commonly used functions

1. atoi() function:

Convert strings to integers .

For example:

char a[] = "1234";

int b = atoi(a);

cout

2. atof() function:

Convert a string into a floating point number.

For example:

char a[] = "12.34";

float b = atof(a);

cout

3. strcmp() function:

Compare the sizes of two strings.

For example:

char str1[] = "Hello";

char str2[] ​​= "World";

int res = strcmp(str1 , str2);

cout

4, strstr() function:

Find in the string Contains another string.

For example:

char haystack[] = "Hello World";

char needle[] = "Wo";

char *res = strstr (haystack, needle);

cout

Note: The return value is a pointer to the first occurrence of the substring.

The above are commonly used string processing techniques in C. I hope they can help everyone. Of course, there are many other techniques and functions that can be used to process strings, which require continuous learning and practice.

The above is the detailed content of String processing techniques in C++. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template