「C++入門」size()で文字列の長さを返すサンプル
書式
文字列.size()
使用例
#include <iostream>
using namespace std;
int main() {
string strA = "arkgame";
cout << "半角文字の文字列の長さ" << endl;
cout << strA.size() << endl;
string strB = "";
cout << "空文字の文字列の長さ" << endl;
cout << strB.size() << endl;
string strC = "テスト";
cout << "全角文字の文字列の長さ" << endl;
cout << strC.size() << endl;
string strD = " ";
cout << "半角空白文字の文字列の長さ" << endl;
cout << strD.size() << endl;
string strE = " ";
cout << "全角空白文字の文字列の長さ" << endl;
cout << strE.size() << endl;
return 0;
}
実行結果
半角文字の文字列の長さ
7
空文字の文字列の長さ
0
全角文字の文字列の長さ
9
半角空白文字の文字列の長さ
1
全角空白文字の文字列の長さ
3