未分類

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 で三角形の合同記号を入力する

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

no image

tex で小文字のL(リットル)記号を入力する方法

tex で小文字のL(l)リットル記号を入力するには、\ell を使う。通常の小文字の l は、\\l であるのに注意。 例 5 \\ell 結果

no image

python で文字列の長さを表示する方法

len を使う。 例 length = len(‘文字列あいうえお’) print(str(length)) 結果 8

no image

C言語で文字をアルファベット順にずらす

C言語で文字をずらすには、char 型の変数に1を加えればよい。 例 #include <stdio.h> int main(void) { char c; c = ‘a’; c++; p …

no image

python3 で、ある文字列で始まるかどうか判定する方法

文章が、ある文字列で始まるかどうかを判定するには startswith を使う。 例 str = “こんにちは。” print(str.startswith(‘こん’)) print(str.star …