要检查字符向量是否包含字符串,可以使用以下方法:
string = "Hello, world!"
substring = "world"
if substring in string:
print("Substring found!")
else:
print("Substring not found.")
std::string::find
函数:#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string substr = "world";
if (str.find(substr) != std::string::npos) {
std::cout << "Substring found!" << std::endl;
} else {
std::cout << "Substring not found." << std::endl;
}
return 0;
}
RegExp.test
方法:const str = "Hello, world!";
const regex = /world/;
if (regex.test(str)) {
console.log("Substring found!");
} else {
console.log("Substring not found.");
}
以上是一些常见的方法来检查字符向量是否包含字符串。具体使用哪种方法取决于你所使用的编程语言和具体的需求。
领取专属 10元无门槛券
手把手带您无忧上云