How to remove spaces before and after a string (C/C ++) (STL)

Source: Internet
Author: User

Abstract
How can I remove the blank spaces before and after the string? (String. find_first_not_of, String. find_last_not_of) (C/C ++) (STL) can be used to remove the spaces before and after the string.ProgramIt is quite streamlined. In this case, the whitespace can be removed and written in template.

Introduction
The original version of the program can be used in vc8, but cannot be used in Dev-C ++. It has been corrected.

Stringtrim1.cpp/C ++

1 /*  
2 (C) oomusou 2006 Http://oomusou.cnblogs.com
3
4 Filename: stringtrim1.cpp
5 Compiler: Visual C + + 8.0/dev-C ++ 4.9.9.2
6 Description: Demo how to trim string by find_first_not_of & find_last_not_of
7 Release: 07/15/2008
8 */
9 # Include < String >
10 # Include < Iostream >
11 # Include < Cwctype >
12
13 Using   Namespace STD;
14
15 String & Trim ( String & S ){
16 If (S. Empty ()){
17 Return S;
18 }
19
20 String : Iterator C;
21 // Erase whitespace before the string
22 For (C = S. Begin (); c ! = S. End () && Iswspace ( * C ++ ););
23 S. Erase (S. Begin (), -- C );
24
25 // Erase whitespace after the string
26 For (C = S. End (); c ! = S. Begin () && Iswspace ( *-- C ););
27 S. Erase ( ++ C, S. End ());
28
29 Return S;
30 }
31
32 Int Main (){
33 String S =   " Hello world !! " ;
34 Cout < S <   " Size: "   < S. Size () < Endl;
35 Cout < Trim (s) <   " Size: "   < Trim (s). Size () < Endl;
36 }

Rows 22 and 23

// Erase whitespace before the string
For (C = S. Begin (); c ! = S. End () && Iswspace ( * C ++ ););
S. Erase (S. Begin (), -- C );

Is to delete the whitespace before the string.

26 and 27 rows

// Erase whitespace after the string
For (C = S. End (); c ! = S. Begin () && Iswspace ( *-- C ););
S. Erase ( ++ C, S. End ());

Is to delete the whitespace after the string.

Row 22 is a deformed for statement. For expr3 is not written, and increment is written in expr2, for from S. begin (), if not S. the end () end is whitespace, and the result is + 1 after the whitespace is determined. In fact, the entire for loop is equivalent to a string. find_first_not_of (). Find the first location that is not whitespace.

Row 23 deletes whitespace from S. Begin (), but why is it deleted to -- C? Because 32 rows of for loop have already been added to 1 before it is jumped out of for loop,-1 is required here.

Line 26 is the same as line 27, but it is considered from the string tail.

I admit that this program is not very easy to understand and is full of C style writing, but ++, which is originally a glorious and special character of C. Since C ++ emphasizes multiple codes, c ++ programmers have to be familiar with this method.

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.