To get the length of a string, use the length()
function:
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length();
Try it Yourself »
Tip: You might see some C++ programs that use the size()
function to get the length of a string. This is just an alias of length()
. It is completely up to you if you want to use length()
or size()
:
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!