When I first learned C/C ++, I had always been familiar with string processing functions. Here I will list C/C ++ string processing functions and hope they will be helpful to beginners.
C:
Char st [100];
1. String Length
Strlen (st );
2. String comparison
Strcmp (st1, st2 );
Strncmp (st1, st2, n); compare the first n of st1 and st2.
3. Add
Strcat (st1, st2 );
Strncat (st1, st2, n); n indicates the first n of st2 connected to st1. Do not add '/0' at the end '.
4. Replace
Strcpy (st1, st2 );
Strncpy (st1, st2, n); n indicates to copy the first n of st2 to st1 and add '/0' at the end '.
5. Search
Where = strchr (st, ch) ch is the character to be searched.
Where = strspns (st1, st2); search for strings.
C ++:
<String>
String str;
1. String Length
Len = str. length ();
Len = str. size ();
2. String comparison
Can be directly compared
You can also:
Str1.compare (str2 );
Str1.compare (pos1, len1, str2, pos2, len2); Value: Negative, 0, positive.
The nops length is reached.
3. Add
Str1 + = str2;
Or
Str1.append (str2 );
Str1.append (str2.pos2, len2 );
4. String Extraction
Str2 = str1.substr ();
Str2 = str1.substr (pos1 );
Str2 = str1.substr (pos1, len1 );
5. string SEARCH
Where = str1.find (str2 );
Where = str1.find (str2, pos1); pos1 starts from the nth digit of str1.
Where = str1.rfind (str2); search from the back.
6. Insert a string
It is not a value assignment statement.
Str1.insert (pos1, str2 );
Str1.insert (pos1, str2, pos2, len2 );
Str1.insert (pos1, numchar, char); numchar indicates the number of inserts and char indicates the characters to be inserted.
7. Replace string
Str1.replace (pos1, str2 );
Str1.replace (pos1, str2, pos2, len2 );
8. delete a string
Str. erase (pos, len)
Str. clear ();
9. Exchange strings
Swap (str1, str2 );
10. C --> C ++
Char * cstr = "Hello ";
String str1;
Cstr = cstr;
String str2 (cstr)