未分類

c++ で文字列が他の文字列を含むか判定する方法

投稿日:

文字列を含むかどうか判定するには、string の find を使う。

見つからなかった場合は、std::string::npos を返す。

#include <iostream>
#include <string>

using namespace std;
int main ()
{
string str1 = "おはようございます";
string str2 = "よう";

int found = str1.find(str2);

if (found != std::string::npos) {
std::cout << "位置: " << int(found) << std::endl;
}
return 0;
}

結果

位置: 6

-未分類

執筆者:


comment

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

関連記事

no image

tex で三角形の相似記号を入力する

\sim を使う。 例 $\triangle{ABC} \sim \triangle{DEF}$ 結果

no image

tex で、表のキャプション内で改行する方法

\newline を使って次のように書く。 例 \begin{table} \begin{tabular}{ |c|c|c|c| } \hline 1 & 2 & 3 \\ \hlin …

no image

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

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

no image

python3 で文字列の最後の文字を削除する方法

文字列の最後に [:-1] を付けると、最後の文字を削除することができる。 例 res = ‘あいうえお'[:-1] print(res) 結果 あいうえ

no image

mac の convert コマンドで画像ファイルをサイズ変更する

convert コマンドを使うには、imagemagick というソフトがインストールされていることが必要。 インストールの確認 $ which convert で、imagemagick がインスト …