Simple knowledge-about string

Source: Internet
Author: User
Toupper, tolower
We all know that C ++'s string does not have toupper. Fortunately, this is not a big problem, because we have STL algorithms:

String s ("heLLo ");
Transform (s. begin (), s. end (), s. begin (), toupper );
Cout <s <endl;
Transform (s. begin (), s. end (), s. begin (), tolower );
Cout <s <endl;

Of course, I know many people want s. to_upper (), but for such a general basic_string, it is indeed impossible to put these proprietary methods in. If you use boost stringalgo, you do not need to read this article.

------------------------------------------------------------------------
Trim
We also know that string does not have trim, but self-reliance is not difficult, it is simpler than toupper:

String s ("hello ");
S. erase (0, s. find_first_not_of ("/n "));
Cout <s <endl;
S. erase (s. find_last_not_of ('') + 1 );
Cout <s <endl;

Note that both find_first_not_of and find_last_not_of can accept strings. In this case, they can find the absence of all characters in the string, so you can trim multiple characters at a time.

-----------------------------------------------------------------------
Erase
The erase of the string itself is good, but it can only be consecutive characters. What if you want to remove all the characters in a string? Use erase + remove_if of STL. Note that remove_if cannot be used.

String s ("hello, world. say bye ");
S. erase (remove_if (s. begin (), s. end (),
Bind2nd (performance_to <char> (),'')),
S. end ());

The above section will remove all spaces and get hello, world. saybye.

-----------------------------------------------------------------------
Replace
String itself provides replace, but it is not string-oriented. For example, we need to make a small combination to replace one substr with another:

String S ("Hello, world ");
String sub ("ello ,");
S. Replace (S. Find (sub), sub. Size (), "appy ");
Cout <S <Endl;

Output is happy world. Note that the original substr is not necessarily the same length as the replaced substr.

-----------------------------------------------------------------------
Startwith, endwith
These two methods are very common, but if you look at the string interface carefully, you will find that there is no need to specifically provide these two methods, and the existing interfaces can do well:

String S ("Hello, world ");
String head ("hello ");
String tail ("LD ");
Bool startwith = S. Compare (0, head. Size (), head) = 0;
Cout <boolalpha <startwith <Endl;
Bool endwith = S. Compare (S. Size ()-tail. Size (), tail. Size (), tail) = 0;
Cout <boolalpha <endwith <endl;

Of course, it's not as convenient as s. startwith ("hello.

------------------------------------------------------------------------
Toint, todouble, tobool...
This is also an old saying. Both the C method and the C ++ method can have their own characteristics:

String s ("123 ");
Int I = atoi (s. c_str ());
Cout <I <endl;

Int ii;
Stringstream (s)> ii;
Cout <ii <endl;

String sd ("12.3 ");
Double d = atof (sd. c_str ());
Cout <d <endl;

Double dd;
Stringstream (sd)> dd;
Cout <dd <endl;

String sb ("true ");
Bool B;
Stringstream (sb)> boolalpha> B;
Cout <boolalpha <B <endl;

The C method is very concise, and the assignment and conversion are completed in one sentence, while the C ++ method is very common.

------------------------------------------------------------------------
Split
This is a headache. What we hope most is an interface: s. split (vect ,','). It is difficult to use the STL algorithm. We can start with a simple one. If the separator is space, tab, and press enter, then this is enough:

String s ("hello world, bye .");
Vector <string> vect;
Vect. assign (
Istream_iterator <string> (stringstream (s )),
Istream_iterator <string> ()
);

However, note that if s is large, it will be more efficient, because stringstream will copy a string for its own use.

------------------------------------------------------------------------
Concat
How to connect all strings in a container containing strings? Hope you don't say it's a hand code loop. Isn't it better?

Vector <string> vect;
Vect. push_back ("hello ");
Vect. push_back (",");
Vect. push_back ("world ");

Cout <accumulate (vect. begin (), vect. end (), string (""));

However, there is room for optimization in terms of efficiency.

-------------------------------------------------------------------------

Reverse
In fact, I suspect someone really needs to reverse a string, but it is really easy to do this:

STD: reverse (S. Begin (), S. End ());

The above is the method of in-situ inversion. If you need to reverse to another string, it is as simple:

S1.assign (S. rbegin (), S. rend ());

The efficiency is also quite satisfactory.

-------------------------------------------------------------------------

Parse file extension
Multiple words:

STD: String filename ("hello.exe ");

STD: String: size_type Pos = filename. rfind ('.');
STD: String ext = filename. substr (Pos = STD: String: NPOs? Filename. Length (): POS + 1 );

But what if two rows are merged into one row? Or not:

STD: String ext = filename. substr (filename. rfind ('.') = STD: String: NPOs? Filename. Length (): Filename. rfind ('.') + 1 );

I know that rfind has been executed twice. But first, you can want the compiler to optimize it. Second, the extension is generally very short. Even if you execute it once more, the difference should be quite small.

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.