C/C ++ string processing functions

Source: Internet
Author: User

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)

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.