When we first use C language to process strings, it will be very troublesome. There is a lack of corresponding string processing functions in the C language. If we want to implement a certain string function, we can only do it ourselves. But when it comes to C++, string processing becomes extremely simple. Today we will learn about the most frequently used string processing functions in C++. Sample code is uploaded to: https://github.com/chenyufeng1991/CppString.
First, introduce the string header file in C++:
#include <string>
Please note that the header file here does not have .h, otherwise it will become header files in C language.
(1) Create a string
There are several construction methods for creating a string. The most typical method is to use the copy constructor,
string str("chenyufeng",3); cout << str << endl;
cout copies the latest 3 characters at the beginning of the original string. The print result is che.
string str2("chenyufeng",2,3); cout << str2 << endl;
cout Copy the latest 3 characters from the beginning of the original string index=2. The printed result is eny.
// = :字符串赋值 str2 = "Robert"; cout << str2 << endl;
You can also assign a string to a variable using direct assignment, using "=". The print result is Robert.
(2) swap: exchange the values of two strings
// swap:交换两个字符串的值 string string1 = "chen"; string string2 = "yufeng"; swap(string1, string2); cout << "string1 = " << string1 << ";string2 = " << string2 << endl;
Print result It has been exchanged with the original string value.
(3)+, append: add a string
// += ,append:在尾部添加字符串 string stringOrigin = "chen"; string stringAppend = "yufeng"; stringOrigin = stringOrigin + stringAppend; cout << "stringOrigin = " << stringOrigin << endl; stringOrigin.append("_OK"); cout << "stringOriginAppend = " << stringOrigin << endl;
Note that adding a string will modify it The original string. You can directly use the + sign to add strings, which is very convenient.
(4) insert: Insert a string at the specified position
// insert:在指定position插入字符串 string stringInsertOrigin = "chenyufeng"; stringInsertOrigin.insert(3, "__"); cout << "stringInsertOrigin = " << stringInsertOrigin << endl;
The above code can be at the indx=3 position Insert __underscore, and the printed result is che__nyufeng.
(5) erase, clear to delete the string
// erase: 删除字符 string stringEraseOrigin = "chenyufeng"; stringEraseOrigin.erase(); cout << "stringEraseOrigin = " << stringEraseOrigin << endl; // clear :删除全部字符 string stringClearOrigin = "chenyufeng"; stringClearOrigin.clear(); cout << "stringClearOrigin = " << stringClearOrigin << endl;
The above operations actually clear the string.
(6) replace: replace the string
// replace: 替换字符串,某个pos位置开始的size个字符替换成后面的“”字符串 string stringReplaceOrigin = "chenyufeng"; stringReplaceOrigin.replace(3, 2, "66"); cout << "stringReplaceOrigin = " << stringReplaceOrigin << endl;
The above code starts the string from index=3 Replace 2 characters with "66", and the printed result is che66ufeng.
(7)==,, =: Compare string sizes
Using this kind of operator to operate on strings in C++ actually uses operator overloading. The size comparison of strings is based on the dictionary order of letters or the ASCII code value. Stop until different letters of the two strings are compared or the last digit of a certain string is compared.
// ==,<,>,<=,>=:比较字符串 string stringLeft = "zhen"; string stringRight = "yufeng"; if (stringLeft == stringRight) { cout << "equal" << endl; } if (stringLeft != stringRight) { cout << "not equal" << endl; } if (stringLeft < stringRight) { cout << "stringLeft < stringRight" << endl; } if (stringLeft > stringRight) { cout << "stringLeft > stringRight" << endl; }
(8) size, length: Calculate the string length
The calculation of the string length here and C Different in languages, the end is not included and the real length is calculated.
// size(), length():计算字符串长度 string stringCount = "chenyufeng"; cout << "stringSize = " << stringCount.size() << endl; cout << "stringLength = " << stringCount.length() << endl;
The above print results are all 10.
(9) empty: Determine whether the string is empty
// empty():判断字符串是否为空 string stringIsEmpty = ""; string stringNotEmpty = "chen"; if (stringIsEmpty.empty()) { cout << "stringIsEmpty == empty" << endl; } else { cout << "stringIsEmpty != empty" << endl; } if (stringNotEmpty.empty()) { cout << "stringNotEmpty == empty" << endl; } else { cout << "stringNotEmpty != empty" << endl; }
(10) String input and output stream
// 输入输出stream cout << "请输入一个字符串"<<endl; string stringInput; cin >> stringInput; cout << "stringInput = " << stringInput << endl;
Strings can also be similar to other data in C++ The same type uses input and output streams. The input stream can be ended using the Enter key.
(11) max_size: The maximum capacity of the string.
// max_size: string stringMaxSize; cout << "stringMaxSize = " << stringMaxSize.max_size() << endl;
The printed result is: 18446744073709551599. Indicates that the string can hold this many characters.
(12)[], at: element access and modification
// [],at() :元素存取 string stringAt = "chenyufeng"; cout << "stringAt[3] = " <<stringAt[3] << endl; cout << "stringAt.at(3) = " << stringAt.at(3) << endl; stringAt[3] = '6'; stringAt.at(5) = '9'; cout << "stringAt = " << stringAt << endl;
Strings can be the same as arrays Perform operations, use subscripts to access, and modify the original string.
(13) compare: Comparison of strings, returns 0, 1, -1.
// compare() string stringCompare = "chenyufeng"; int aaa = stringCompare.compare("chen"); // > 0 int bbb = stringCompare.compare("chenyufeng"); // == 0 int ccc = stringCompare.compare("done"); // < 0 cout << "aaa = " << aaa << ";bbb = " << bbb << ";ccc = " << ccc << endl;
(14) substr: Get a substring
// substr string stringSubstr = "chenyufeng"; // 从索引为4开始的3个字符 cout << "stringSubstr.substr(4,3) = " << stringSubstr.substr(4,3) << endl; // 从索引为4开始的所有字符 cout << "stringSubstr.substr(4) = " <<stringSubstr.substr(4) << endl; // 整个字符 cout << "stringSubstr.substr() = " <<stringSubstr.substr() << endl;
(15) find: Find a certain character
// find string stringFind = "chenyufeng"; stringFind.find('n'); cout << "stringFind.find('n') = " << stringFind.find('n') << endl; cout << "stringFind.find_first_of('e') = " << stringFind.find_first_of('e') << endl; cout << "stringFind.find_last_of('e') = " << stringFind.find_last_of('e') << endl;
The default find function returns the index of the first occurrence of a character. find_first_of and find_last_of are the indexes of the first and last occurrence of a certain character respectively.
The above is the content of the use of strings in C++. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!