How do I check if string contains substring?
Like this:
if (str.indexOf("Yes") >= 0)
...or you can use the tilde operator:
if (~str.indexOf("Yes"))
This works because indexOf()
returns -1
if the string wasn't found at all.
Note that this is case-sensitive.
If you want a case-insensitive search, you can write
if (str.toLowerCase().indexOf("yes") >= 0)
Or:
if (/yes/i.test(str))
The latter is a regular expression or regex.
Regex breakdown:
/
indicates this is a regexyes
means that the regex will find those exact characters in that exact order/
ends the regexi
sets the regex as case-insensitive.test(str)
determines if the regular expression matchesstr
To sum it up, it means it will see if it can find the lettersy
,e
, ands
in that exact order, case-insensitively, in the variablestr
How to check whether a string contains a substring in JavaScript?
ECMAScript 6 introduced String.prototype.includes
:
const string = "foo";
const substring = "oo";
console.log(string.includes(substring)); // true
Go test string contains substring
Use the function Contains
from the strings package.
import (
"strings"
)
strings.Contains("something", "some") // true
How to check whether a string contains a substring in Kotlin?
Kotlin has stdlib
package to perform certain extension function operation over the string, you can check this method it will check the substring in a string, you can ignore the case by passing true/false value. Refer this link
"AbBaCca".contains("bac", ignoreCase = true)
How to check if a string contains a substring in Bash
You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
Note that spaces in the needle string need to be placed between double quotes, and the *
wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==
), not the regex operator =~
.
How to check if a string contains a substring in Rust?
Rust has no such operator. You can use the String::contains
method instead:
if a.contains("bc") {
do_something();
}
How to see if a string ONLY contains a substring in python
No regex necessary. Relying on the fact that str.count
counts non-overlapping occurrences
len(target) * data.count(target) == len(data)
Simple string methods are 400-800% faster than regex here:
>>> import re
>>> target = "World"
>>> data = "World" * 3
>>> pattern = f"^({re.escape(target)})+$"
>>> %timeit len(target) * data.count(target) == len(data)
115 ns ± 0.352 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
>>> %timeit re.match(pattern, data) is not None
456 ns ± 2.88 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
>>> %timeit bool(data.replace(target, '')) # str.replace is faster again
51.7 ns ± 0.269 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
How to check whether a string contains a substring in Ruby
You can use the include?
method:
my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
end
Related Topics
Generate HTML Table from 2D JavaScript Array
Is It Evil to Use Eval to Convert a String to a Function
How Is JavaScript Single Threaded
How to Check If an Object Has a Key in JavaScript
What Is the List of Possible Values for Navigator.Platform as of Today
Differencebetween Typeof and Instanceof and When Should One Be Used VS. the Other
How to Intercept All Ajax Requests Made by Different Js Libraries
How to Remove a Key from a JavaScript Object
Check If Jquery Has Been Loaded, Then Load It If False
Google Map API V3 - Set Bounds and Center
How to Copy JavaScript Object to New Variable Not by Reference
What Does the Function Then() Mean in JavaScript
How to Replace Captured Groups Only
Array.Length Is Zero, But the Array Has Elements in It
How to Check If the Url Contains a Given String