How to Find Substring from String

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

Python: Find a substring in a string and returning the index of the substring

Ideally you would use str.find or str.index like demented hedgehog said. But you said you can't ...

Your problem is your code searches only for the first character of your search string which(the first one) is at index 2.

You are basically saying if char[0] is in s, increment index until ch == char[0] which returned 3 when I tested it but it was still wrong. Here's a way to do it.

def find_str(s, char):
index = 0

if char in s:
c = char[0]
for ch in s:
if ch == c:
if s[index:index+len(char)] == char:
return index

index += 1

return -1

print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))

It produced the following output:

3
8
-1

Check substring exists in a string in C

if (strstr(sent, word) != NULL) {
/* ... */
}

Note that strstr returns a pointer to the start of the word in sent if the word word is found.

Find substring in string without includes/indexOf/Regex in Javascript

Here is an optimized example of the algorythm isSubstring. It iterates only through the minimum number of characters required.

For example, if the string is 20 characters long and the substring is only 5 characters long, when we get to the 16th position of the string we can assume that the substring doesn't exist within the string (16 + 5 = 21 > 20)

function isSubstring(str, sub){  if(sub.length > str.length) return false;  for(let i = 0; i < str.length - sub.length + 1; i++){    if(str[i] !== sub[0]) continue;    let exists = true;    for(let j = 1; j < sub.length && exists; j++){        if(str[i+j] === sub[j]) continue;        exists = false;    }    if(exists) return true;  }  return false;}
//expected trueconsole.log(isSubstring("hello world", "hello"));console.log(isSubstring("hello world", "world"));console.log(isSubstring("hello world", "d"));console.log(isSubstring("hello world", "o w"));console.log(isSubstring("hello world", "rl"));console.log(isSubstring("hello world", ""));
//expected falseconsole.log(isSubstring("hello world", "hello world 1"));console.log(isSubstring("hello world", "helloo"));

Does Python have a string 'contains' substring method?

Use the in operator:

if "blah" not in somestring: 
continue

Go test string contains substring

Use the function Contains from the strings package.

import (
"strings"
)
strings.Contains("something", "some") // true

How to find all occurrences of a substring?

There is no simple built-in string function that does what you're looking for, but you could use the more powerful regular expressions:

import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]

If you want to find overlapping matches, lookahead will do that:

[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]

If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:

search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]

re.finditer returns a generator, so you could change the [] in the above to () to get a generator instead of a list which will be more efficient if you're only iterating through the results once.

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



Leave a reply



Submit