未分類

c++ で文字列(string)の長さを取得する方法

投稿日:

length() で取得する。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string str1 = "Good morning";
cout << str1.length() << endl;
return 0;
}

結果

12

参考

上記のコードでは、バイト数を取得している。
全角文字列の場合には、注意が必要。

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

tex でかけ算の記号 x (✕)を表示する

かけ算記号を表示するには、$\times$ を使う。 例 $2 \times 3$ 結果

no image

python3 で、文字列の最後が特定の文字で終わるか判定する方法

endswith を使う。 例 str1 = “こんにちは” result1 = str1.endswith(“は”) result2 = str1.endswith(“わ”) print(resul …

no image

c++ で文字列をファイルに保存する方法

result.txt というファイルに “data1 data2 data3” という内容を保存する。ofstream を使って書き出す。 例 #include<iost …

no image

tex で脚注を表示する方法

\footnote を使う。 例 \footnote{this is a footnote.} 結果

no image

tex でファイ(φ、Φ)を入力する方法

小文字のファイ:φ、大文字のファイ:Φ串刺しでないファイ($\varphi$)も使われることがある。 例 $\phi, \varphi, \Phi$ 結果