未分類

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 で期待値記号を入力する方法

\mathbf を使う。 例 $\mathbf{E}(\xi)$ 結果

no image

tex で ξ 記号を表示する

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

no image

python3 で文字列のリストを結合して1つの文字列にする方法

リストに対して、join メソッドを実行する。 例 list1 = [‘hello, ‘,’good ‘, ‘morning’] str1 = ”.join(list1) print(str1) 結 …

no image

tex で同値記号(⇔)を入力する方法

同値記号を入力するには、\Leftrightarrow を使う。 例 $b \leftrightarrow c$, $b \Leftrightarrow c$

no image

matplotlib で x, y軸・タイトルのフォントサイズを調整する

ラベルの fontsize で指定する。 例 import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2, 100) …