未分類

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 で文字色を RGB で指定する

文字色を RGB で指定する。例では、\definecolor で、「samplecolor」という色を定義して使っている。 例 \usepackage{xcolor}\begin{document} …

no image

python3 で range からリストを生成する方法

list をrange を指定して初期化すれば良い。数を1つ飛ばしのリストも次のようにして作れる。 例 list1 = list(range(1,10,2)) print(list1) 結果 [1, …

no image

python3 で、ファイルの文字列の1行目を出力する

ファイルを読み込んで、1行目だけをコンソールに出力するには readlines() を使う。 例 f = open(“data.txt”) lines = f.readlines() print(li …

no image

tex で ξ 記号を表示する

ξ はギリシャ文字のクシー(xi)である。ゼータ(ζ)と間違えないように注意。 例 $\xi$ 結果

no image

tex で比例記号を入力する

数学で、y と x が比例するとき「比例記号」を使って表す方法がある。tex で表すには次のようにする。 例 $$ y \propto x $$ 結果 あなたがしてくれなくても 1【電子書籍】[ ハル …