length() で取得する。
例
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Good morning";
cout << str1.length() << endl;
return 0;
}
結果
12
参考
上記のコードでは、バイト数を取得している。 全角文字列の場合には、注意が必要。
雑記
投稿日:
length() で取得する。
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str1 = "Good morning";
cout << str1.length() << endl;
return 0;
}
12
上記のコードでは、バイト数を取得している。 全角文字列の場合には、注意が必要。
執筆者:seyanen
関連記事
python3 で辞書(dict) の for ループでキーと値を取得する
キーと値を取得するには次のようにする。キーは dict のループ、値は dict の values() のループで取得できる。 例 mydictionary = {‘color’: ‘白’, ‘ani …
python3 で文字列を区切り文字を指定してリストに変換する方法
split メソッドを使う。 例 str1 = ‘this, is, a, pen’ str2 = str1.split(‘,’) print(str2) 結果 [‘this’, ‘ is’, ‘ a …
2023/01/18
matplotlib のグラフ作成と gnuplot との対応 比較
2022/10/14
pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。