Cplusplus 简明教程
C++ String Length
string 的长度是字符串中存在的字符数。这些字符可以是 char 数据类型,其中包括所有字母数字元素、符号和杂项字符。在 C++ programming language 中,有两种类型的字符串- Character Arrays 的 C 样式类型,以及 String objects 作为 <string> class 的内置对象。
Length of a string is the number of characters present in the string. These characters can be of the data type char, and these include all alphanumeric elements, symbols and miscellaneous characters. In C++ programming language, there are two types of strings- Character Arrays of C-Style type, and String objects which are built-in objects of <string> class.
字符串的长度也包括空格,但如果字符串包含 terminating character “\0”,则字符串在此字符结束,而且长度的计数字符就在该字符之前终止。
The length of a string also includes white spaces, but in case a string includes a terminating character "\0", the string ends at that character and the count of length is terminated just before that character.
有很多种方法可以查找给定字符串的长度。其中一些方法是迭代的,而另一些方法还使用 in-built functions 和方法。这些方法在本教程的以下部分中进行了明确解释 −
There are many ways to find the length of a given string. Some of these methods are iterative, whereas some also use in-built functions and methods. These methods are explained clearly in the following parts of this chapter −
-
Using strlen() Method
-
Using string::length() Method of String Class
-
Using string::size() Method of String Class
-
Using Iterative for Loop
-
Using Iterative while Loop
String Length Using strlen() Method
字符串被定义为字符数组,该数组使用 pointer 访问数组的第一个迭代器。我们可以使用 C 库的 strlen() method 来计算 C 类型数组的长度。
Strings are defined as character arrays which are accessed using the pointer to the first iterator of the array. We can use strlen() method of C Library to calculate the length of C-type arrays.
String Length Using string::size() Method
大多数程序员在需要计算 C++ 编程语言中字符串长度时 commonly 使用 string::size() method 的 string class 。这是最基本的方法,并且通常在遍历 string object. 时使用
Most programmers commonly use string::size() method of string class when there is a need to calculate the length of a string in C++ programming language. It is the most basic method, and it is generally used while traversing a string object.
Syntax
以下语法演示了如何使用 size() 方法来计算字符串长度 −
The following syntax shows how to use size() method to calculate the length of the string −
string_object.size();
Example
以下示例说明了如何使用 size() 方法计算字符串的长度 −
The following example shows how to calculate the length of the string using size() method −
#include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
cout<<"Length of string s : "<<s.size();
return 0;
}
Length of string s : 13
String Length Using string::length() Method
我们还可以使用 length() method of string class 来确定给定字符串的长度。 length() 和 size() 方法都是 <string> 头文件的一部分,它们被称为字符串对象的作为方法。
We can also use length() method of string class to determine the length of the given string. Both length() and size() methods are part of <string> header file, and these are called as methods to the string object.
Syntax
以下语法演示了如何使用 length() 方法来计算字符串长度 −
The following syntax shows how to use length() method to calculate the length of the string −
string_object.length();
Example
以下示例说明了如何使用 length() 方法计算字符串的长度 −
The following example shows how to calculate the length of the string using length() method −
#include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
cout<<"Length of string s : "<<s.length();
return 0;
}
Length of string s : 13
String Length Using while Loop
我们可以使用一个简单的 while loop 来遍历字符串并初始化变量 count 以计算字符串长度,直到我们到达字符串末尾。对于每次迭代,count 都会增加 1,因此最终结果将是字符串的长度。
We can use a simple while loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string.
Syntax
以下语法演示了如何使用 while 循环来计算字符串长度 −
The following syntax shows how to use a while loop to calculate the length of the string −
while(s[i]!='\0') {
[body]
}
Example
以下示例说明了如何使用单一 while 循环来计算字符串的长度 −
The following example shows how to calculate the length of the string using a single while loop −
#include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
int count=0, i=0;
while(s[i]!='\0')
count++, i++;
cout<<"Length of string s : "<<count;
return 0;
}
Length of string s : 13
String Length Using a for Loop
我们可以使用一个简单的 for loop 来遍历字符串并初始化变量 count 以计算字符串长度,直到我们到达字符串末尾。对于每次迭代,count 都会增加 1,因此最终结果将是字符串的长度。
We can use a simple for loop to iterate over the string and initialize a variable count to calculate the length of the string until we reach the end of the string. For each iteration, the count increases by one, hence the net result will be the length of the string.
Syntax
以下语法演示了如何使用 for 循环来计算字符串长度 −
The following syntax shows how to use a for loop to calculate the length of the string −
for(int i=0;s[i]!='\0';i++){
[body]
}
Example
以下示例说明了如何使用单一 for 循环来计算字符串的长度 −
The following example shows how to calculate the length of the string using a single for loop −
#include <bits/stdc++.h>
using namespace std;
int main() {
string s="I love TP !!!\0 and others";
int count=0;
for(int i=0;s[i]!='\0';i++)
count++;
cout<<"Length of string s : "<<count;
return 0;
}
Length of string s : 13