Introduction to the String class in C + +

Source: Internet
Author: User

In C + +, for the problem of handling strings in the language, a string class was designed in the standard library, so it is now possible to use the string class directly in programming, involving the processing of strings.

The reason to discard char* strings and select the String class in the C + + standard library is because he compares with the former, doesn't have to worry about enough memory, string length, and so on, and as a class, his integrated operations function is enough to do most of our (even 100%) needs. We can use = for assignment operation, = = For comparison, + do series (is not very simple?). We can think of it as the basic data type of C + +.

First, in order to use the string type in our program, we must include the header file <string>. As follows:

#include <string>/Note this is not string.h string.h is a C string header file

1. Declaring a C + + string

Declaring a string variable is simple:

String Str;

So we declare a string variable, but since it is a class, there are constructors and destructors. The above declaration does not pass in parameters, so the default constructor for string is used directly, and this function is to initialize STR to an empty string. The constructors and destructors for the string class are as follows:

a) string s; Generate an empty string s

b) string S (str)//Copy constructors generate a replica of STR

c) string s (str,stridx)//Stridx the part of the string str "beginning with position" as the initial value of the string

d) string s (str,stridx,strlen)//Stridx the part of the string str "beginning with the length of strlen" as the initial value of the string

e) string s (CStr)//The C string as the initial value of s

f) string S (chars,chars_len)//Chars_len The first character of the C string as the initial value of the string s.

g) string S (num,c)//Generate a string containing num C characters

h) string S (beg,end)//The initial value of the string s as a character in the interval beg;end (not including end)

i) s.~string ()//Destroy all characters, free memory

2. String manipulation functions

A) =,assign ()//Assign a new value

b) Swap ()//Exchange two-string contents

c) +=,append (), push_back ()///Add character at tail

D insert ()//Insert character

e) Erase ()///delete character

f) Clear ()//Remove all characters

g) Replace ()//Replace character

h) +//concatenated string

i) ==,!=,<,<=,>,>=,compare ()//comparison string

j) Size (), Length ()//return number of characters

k) max_size ()//returns the possible maximum number of characters

L) empty ()//To determine whether the string is empty

m) Capacity ()//returns the character capacity before redistribution

N) reserve ()//reserve a certain amount of memory to accommodate a certain number of characters

o) [], at ()//Access single character

p) >>,getline ()//reading a value from stream

Q <<//write a value to stream

r) Copy ()//Assign a value to a c_string

s) c_str ()//return content to C_string

T) data ()//returns the content as a character array

u) substr ()//Return of a substring

V) Lookup function

W) begin () end ()//provide iterator support similar to STL

x) Rbegin () rend ()//Reverse iterators

Y) Get_allocator ()//Return Configurator

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.