一、是否包含子字符串
语言 | 代码 |
---|
js | if (str.indexOf(subStr) != -1) |
java | if (str.indexOf(subStr) != -1) |
php | if (strstr($str, $subStr)) |
c | if (strstr(a, c) == NULL) |
shell | if [[ $A == *$B*]] |
python | if (subStr in str) |
JavaScript
if (str.indexOf(subStr) != -1) {
console.log("包含")
} else {
console.log("不包含")
}
python
if (subStr in str) {
print("包含")
} else {
print("不包含")
}
java
if (str.indexOf(subStr) != -1) {
System.out.println("包含")
} else {
System.out.println("不包含")
}
php
<?php
$str = '我的'
$subStr = "我"
if (strstr($str, $subStr)) {
echo "包含";
} else {
echo "不包含";
}
?>
shell
A = "hello world"
B = "low"
if [[ $A == *$B* ]]
then
echo "包含"
else
echo "不包含"
fi
c
#include<stdio.h>
#include<string.h>
int main() {
char *a = "abcdef";
char *b = "def";
char *c = "123";
if (strstr(a, c) == NULL) {
printf("%s", "包含");
} else {
printf("%s", "不包含");
}
return 0;
}