stl string 函数_在C ++ STL中带有示例的string :: length()函数

stl string 函数

字符串作为数据类型 (String as datatype)

In C, we know string basically a character array terminated by ‘\0’. Thus to operate with the string we define character array. But in C++, the standard library gives us the facility to use the string as a basic data type as an integer. We can easily find the length of the string using length() function.

在C语言中,我们知道字符串基本上是一个以'\ 0'结尾的字符数组。 因此,要对字符串进行操作,我们定义了字符数组。 但是在C ++中,标准库为我们提供了将字符串用作基本数据类型(作为整数)的便利。 我们可以使用length()函数轻松地找到字符串的长度。

Prototype:

原型:

    size_t string.length();

Parameter: None

参数:

Return type: size_t

返回类型: size_t

Example:

例:

    Like we define and declare,

    string s1="Include", s2="Help";
    
    int i=s1.length(); //7
    int j=s2.length(); //4

    After concatenating:
    
    string s3=s1+s2;
    (s3 is "IcludeHelp")
    int k=s3.length(); //11

    string s4=s2+s1;
    (s4 is "HelpInclude")
    int r=s4.length(); //11


Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.

请记住,需要在“”下定义一个字符串变量(文字)。 “ a”是字符,而“ a”是字符串。

Header file needed:

所需的头文件:

    #include <string>
    Or
    #include <bits/stdc++.h>

C++ program to demonstrate example of string::length() function

C ++程序演示string :: length()函数的示例

#include <bits/stdc++.h>
using namespace std;

int main(){
	string s;
	
	cout<<"enter string\n";
	cin>>s;
	
	cout<<"length of the string is: "<<s.length();
	
	return 0;
}

Output

输出量

enter string
IncludeHelp
length of the string is: 11


翻译自: https://www.includehelp.com/stl/string-length-function-with-example-in-cpp-stl.aspx

stl string 函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
StringC++ STL(标准模板库)的一个类,用于表示和操作字符串String 类提供了许多函数和操作符,使得处理字符串变得更加方便和高效。 要使用 string,需要包含头文件 `<string>`。以下是一些常见的 string 操作及示例: 1. 创建 string: ```cpp std::string myString; // 创建一个空的 string std::string myString = "Hello"; // 创建并初始化一个 string ``` 2. 获取 string 的长度: ```cpp int length = myString.length(); // 获取 string 的长度 ``` 3. 连接两个 string: ```cpp std::string str1 = "Hello"; std::string str2 = "World"; std::string result = str1 + str2; // 连接两个 string ``` 4. 访问和修改 string 的字符: ```cpp char ch = myString[0]; // 获取指定位置的字符 myString[0] = 'H'; // 修改指定位置的字符 ``` 5. 查找子字符串: ```cpp size_t found = myString.find("lo"); // 查找子字符串的位置 if (found != std::string::npos) { // 子字符串存在 } ``` 6. 截取子字符串: ```cpp std::string subString = myString.substr(3, 4); // 截取指定位置和长度的子字符串 ``` 7. 比较两个 string: ```cpp int compareResult = str1.compare(str2); // 比较两个 string 的大小 if (compareResult == 0) { // 两个 string 相等 } ``` 除了上述操作外,string 类还提供了许多其他功能,如插入、删除、替换、转换等。 使用 string 类可以更方便地处理字符串,同时避免了手动管理内存和处理字符数组的复杂性。 这只是 string 的一些基本用法,还有更多的功能和操作可以探索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值