C++ Strings
C++ provides the following two types of string representations:
- C-style Strings
- The string class type introduced by C++
C-style Strings
C-style strings originated from the C language and continue to be supported in C++. A string is actually a one-dimensional null character array terminated by \0. Therefore, a null-terminated string contains the characters that make up the string.
The declaration and initialization below creates a RUNOOB string. Since a null character is stored at the end of the array, the size of the character array is one greater than the number of characters in the word RUNOOB .
char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'};
According to the array initialization rules, you can write the above statement as the following:
char site[] = "RUNOOB";
The following is the memory representation of a string defined in C/C++:
In fact, you do not need to put the null character at the end of a string constant. The C++ compiler automatically places \0 at the end of the string when initializing the array. Let us try to output the string above:
Example
#include
<
iostream
using
namespace
std;
int
main
(
)
{
char
site
[
7
]
=
{
'
R
', '
U
', '
N
', '
O
', '
O
', '
B
', '\
0
'
}
;
cout
<<
"
Runoob Tutorial:
"
;
cout
<<
site
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
菜鸟教程: RUNOOB
C++ has a large number of functions to operate on null-terminated strings:
| No. | Function & Purpose |
|---|---|
| 1 | strcpy(s1, s2); |
| Copies string s2 to string s1. | |
| 2 | strcat(s1, s2); |
连接字符串 s2 到字符串 s1 的末尾。连接字符串也可以用 + 号,例如:
string str1 = "runoob";
string str2 = "google"; string str = str1 + str2; | | 3 | strlen(s1);
Returns the length of string s1. | | 4 | strcmp(s1, s2);
If s1 and s2 are identical, return 0; if s1
Returns a pointer to the first occurrence of character ch in string s1. | | 6 | strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1. |
The following example uses some of the functions described above:
Example
#include
<
iostream
#include
<
cstring
using
namespace
std;
int
main
(
)
{
char
str1
[
13
]
=
"
runoob
"
;
char
str2
[
13
]
=
"
"
;
char
str3
[
13
]
;
int
len
;
// Copy str1 to str3
strcpy
(
str3
,
str1
)
;
cout
<<
"
strcpy( str3, str1) :
"
<<
str3
<<
endl
;
// Concatenate str1 and str2
strcat
(
str1
,
str2
)
;
cout
<<
"
strcat( str1, str2):
"
<<
str1
<<
endl
;
// Total length of str1 after concatenation
len
=
strlen
(
str1
)
;
cout
<<
"
strlen(str1) :
"
<<
len
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
strcpy( str3, str1) : runoob
strcat( str1, str2): runoobgoogle
strlen(str1) : 12
The String Class in C++
The C++ standard library provides the string string class type, which supports all the operations described above and adds more functionality. We will study this class from the C++ standard library; first, let us look at the following example:
You may not fully understand this example yet, because we have not discussed classes and objects so far. So you can just skim this example for now, and come back to it after you understand the concept of object orientation.
Example
#include
<
iostream
#include
<
string
using
namespace
std;
int
main
(
)
{
string
str1
=
"
runoob
"
;
string
str2
=
"
"
;
string
str3
;
int
len
;
// Copy str1 to str3
str3
=
str1
;
cout
<<
"
str3 :
"
<<
str3
<<
endl
;
// Concatenate str1 and str2
str3
=
str1
str2
;
cout
<<
"
str1 + str2 :
"
<<
str3
<<
endl
;
// Total length of str3 after concatenation
len
=
str3
.
size
(
)
;
cout
<<
"
str3.size() :
"
<<
len
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
str3 : runoob
str1 + str2 : runoobgoogle
str3.size() : 12