This is my seventh day of the August Genwen Challenge


A preface.

Char a[] = “Hello world”; char a[] = “hello world”; Char * b = “hello world”; char * b = “hello world”; Enter the string scanf(“%s”, a); Cout << a << endl; cout << a << endl; cout << b << endl; If we want to add two strings: strcat(a, a); Don’t ask me why I don’t use strcat of a,b; Because the program blows up. cout << a << endl; Or copy: strcpy(a, a); The above is the C-style string, C++ standard library added the string class, string string than the C language string more convenient, more powerful, more secure. Since it is a superset of C, how can there not be something new to replace C? Heh heh.


String String (Thesis)

1. String initialization, assignment, concatenation, and attachment

Moving on, the string type is defined in the string header.

 string str_1 = "hello world";  // Copy initialization
 string str_2 = { "hello world" }; // Copy initialization
 string str_3("hello world");  // Direct initialization
 string str_4{ "hello world" };  // Direct initialization
 // You can use any of the above methods to initialize a string, and string strings do not hold '\0'. Strings have their own member functions
 // It is used to record the size of the string, so it does not judge the end of the string
 / / assignment
 string str;
 char a[20] = { "abcde" };
 str = a; 
 cout << str << endl;
 char *b = { "eeee" };
 str = b;
 // Splice attach
 cout << str << endl;
 str = str + a;
 cout << str << endl;
 str = str + b;
 cout << str << endl;
 
Copy the code

The fact is that string is much more convenient, much more secure, much more powerful, and string is compatible with C strings

Running results:


2. String length functions

// before C++ added strings, strcpy() was used to concatenate strings, strcat() was used to attach strings
 // We can also use the above function on string objects, and there is a problem with string assignment in C:
 char site[10] = { "abcdef" };
 char site_2[10] = { "aaaaa" };
 //strcat(site, site_2);
 // An error will occur and the program will crash because it cannot store information beyond the target size
 Strncat (); strcpy(); strncat()
 Strlen ();
 // The new String class provides many member functions to simplify tedious operations
 // String length function:
 string str = "Take the dog";
 cout << str.size(a); cout << str.length(a);Copy the code

Size () and length() are the same, both return the number of bytes of the string! [insert picture description here] (img – blog. Csdnimg. Cn / 20200408214… =300x)

Running results:


3. Convert string to char *

 const char * c = str.c_str(a);// Returns a string ending in '\0'
 const char * ca = str.data(a);// There is no '\0' in the string produced
 cout << c << endl;
 cout << ca << endl;
Copy the code

The c_str() function returns a string ending in ‘\0’, while data() has no standard. [insert picture description here] (img – blog. Csdnimg. Cn / 20200408213… =600x)

Return pointer to null-terminated nonmutable array indicates that c_str returns a null-terminated string, but data does not mention this.


4. Check whether the string object is empty

string str;
if(str.empty())
{
/ /...
}
Copy the code

5. Convert int to string

#include<sstream>
 int number = 123;
 stringstream s;
 s << number;
 string str_1 = s.str(a);/ / the first
 s >> str_1; // This can also be done
 cout << str_1;
 / / the second
 int number_2 = 123;
 string str_3;
 str_3 = to_string(number_2);
 cout << str_3;
Copy the code

We’re not just talking about ints here, we’re talking about integers and floating-point types, and since we’re talking about stringstreams, here’s an example:

6. The string type is changed to int

String STR.while(getline(cin,str))
{
    int num;
    stringstream s(line);
    while(ss>>x) { cout<<x<<endl; }}Copy the code

Readers should see that this is a string transformation, knowledge is the need to learn flexible use, learn to forward, but also to think about the reverse.


7. Insert characters into the string string

 string str = "hello my name huagou ";
 string str_1 = "is "; 
 str.insert(14, str_1);
 cout << str;
 // There are many variations of insert, so you can check them out for yourself
Copy the code

8. Search for the string

 string str = " y hello my name is zhangxv";
 string str_1 = "my";
 if (str.find(str_1)! =string::npos)//find also takes an argument to start the search, which defaults to 0 and returns a subscript
 {
  cout << "Found it.";
 }
 // Are c-style strings more convenient? Both characters and strings are searchable.
 //find returns an integer. If there is an inclusion relation, the return value must not be npos (a constant).
 
 //num = s.ind_first_of (STR) returns the position where STR first appears in the parent string s
 //num = s.ind_last_of (STR) Returns the position where STR last appeared in the parent string s
Copy the code

Find (): finds the position where the character C or s first appears in the string. Find_first_of (): Finds the first occurrence position of any character in the string c and the string array S.

So I’m done with string. Bye bye!