How to Check If Multiple Strings Exist in Another String

Check if multiple strings exist in another string

You can use any:

a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]

if any(x in a_string for x in matches):

Similarly to check if all the strings from the list are found, use all instead of any.

How to check if all multiple strings exist in another string (for all row in dataframe)

this code snippet should do your work:

df = pd.DataFrame({'sub_strings': [["the sun", "rising up"], ["go home", "tomorow"]], 'string': ["the sun is rising up.", "I will go home."]})
df.head()
            sub_strings                 string
0 [the sun, rising up] the sun is rising up.
1 [go home, tomorow] I will go home.
df['value'] = df.apply(lambda x: all([val in x.string for val in x.sub_strings]), axis=1)
df.head()
            sub_strings                 string  value
0 [the sun, rising up] the sun is rising up. True
1 [go home, tomorow] I will go home. False

checking if any of multiple substrings is contained in a string - Python

Here is a possible one-line solution:

print('there is a banned substring inside'
if any(banned_str in url for banned_str in black_list)
else 'no banned substrings inside')

If you prefer a less pythonic approach:

if any(banned_str in url for banned_str in black_list):
print('there is a banned substring inside')
else:
print('no banned substrings inside')

How can I check if multiple strings exist in another string?

Yes, you can use grepl (not grep, actually), but you must run it once for each substring:

> sapply(str, grepl, myStr)
very beauti bt
TRUE TRUE TRUE

To get only one result if all of them are true, use all:

> all(sapply(str, grepl, myStr))
[1] TRUE

Edit:

In case you have more than one string to check, say:

myStrings <- c("I am very beautiful btw", "I am not beautiful btw")

You then run the sapply code, which will return a matrix with one row for each string in myStrings. Apply all on each row:

> apply(sapply(str, grepl, myStrings), 1, all)
[1] TRUE FALSE

How to check if multiple substrings appear together in a string

strings = ['I have a bird', 'I have a bag and a bird', 'I have a bag']
words = ['bird','bag']

for string in strings:
stringlist = string.split()
word1 , word2 = words
if word1 in stringlist and word2 in stringlist:
print(True)
else:
print(False)

Result

False
True
False

How to check if a String contains any of some strings

If you are looking for single characters, you can use String.IndexOfAny().

If you want arbitrary strings, then I'm not aware of a .NET method to achieve that "directly", although a regular expression would work.

Go: how to check if a string contains multiple substrings?

You can write your own utility function using strings.Contains() that can work for multiple sub-strings.

Here's an example that returns Boolean (true/false) in case of complete / partial match and the total number of matches:

package main

import (
"fmt"
"strings"
)

func checkSubstrings(str string, subs ...string) (bool, int) {

matches := 0
isCompleteMatch := true

fmt.Printf("String: \"%s\", Substrings: %s\n", str, subs)

for _, sub := range subs {
if strings.Contains(str, sub) {
matches += 1
} else {
isCompleteMatch = false
}
}

return isCompleteMatch, matches
}

func main() {
isCompleteMatch1, matches1 := checkSubstrings("Hello abc, xyz, abc", "abc", "xyz")
fmt.Printf("Test 1: { isCompleteMatch: %t, Matches: %d }\n", isCompleteMatch1, matches1)

fmt.Println()

isCompleteMatch2, matches2 := checkSubstrings("Hello abc, abc", "abc", "xyz")
fmt.Printf("Test 2: { isCompleteMatch: %t, Matches: %d }\n", isCompleteMatch2, matches2)
}

Output:

String: "Hello abc, xyz, abc", Substrings: [abc xyz]
Test 1: { isCompleteMatch: true, Matches: 2 }

String: "Hello abc, abc", Substrings: [abc xyz]
Test 2: { isCompleteMatch: false, Matches: 1 }

Here's the live example: https://play.golang.org/p/Xka0KfBrRD

how do i check for multiple strings at once in python?

You can achieve this using regex

import re
ans = input("String to test : ")

if re.search(r"[a-zA-Z]", ans) == None: # Test for lowercase and uppercase letters
print("No letter in this string")
else:
print("Letter found")

However this will not be triggered by é è ù ì etc.



Related Topics



Leave a reply



Submit