此函數用於以字節為單位查找字符串的長度。
這是符合 string 內容的實際字節數,不一定等於存儲容量。
用法
int len = s1.length();
參數
此函數包含單個參數。
返回值
此函數以字節為單位返回整數值。
這裏,s1 是一個字符串,strlen 函數計算字符串 s1 的長度並將其值存儲在 len 變量中。
例子1
#include<iostream>
using namespace std;
int main()
{
string s1 = "Welcome to javatpoint";
int len = s1.length();
cout<< "length of the string is:" << len;
return 0;
}
輸出:
Length of the string is 21
考慮一個字符串 s1 和 s1 包含值,即歡迎使用 javatpoint 。我們使用 strlen 函數計算字符串的長度並存儲在變量 len 中。
相關用法
- C++ String swap()用法及代碼示例
- C++ String back()用法及代碼示例
- C++ String append()用法及代碼示例
- C++ String Assign()用法及代碼示例
- C++ String begin()用法及代碼示例
- C++ String size()用法及代碼示例
- C++ String resize()用法及代碼示例
- C++ String Find()用法及代碼示例
- C++ String crend()用法及代碼示例
- C++ String compare()用法及代碼示例
- C++ String empty()用法及代碼示例
- C++ String replace()用法及代碼示例
- C++ String at()用法及代碼示例
- C++ String insert()用法及代碼示例
- C++ String clear()用法及代碼示例
- C++ String Data()用法及代碼示例
- C++ String cend()用法及代碼示例
- C++ String pop_back()用法及代碼示例
- C++ String find_first_not_of()用法及代碼示例
- C++ String find_last_of()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ String length()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。