String1 = "12345"
String2 = "12abc"
I want to return true
if String1
contains some part of String2
. In this example String2 has "12" which exists in String1. So For example String1.Contains(String2)
should return true
.
How Can I make such 'Contains'
Function?
The function strstr returns the first occurrence of a string in another string. This means that strstr can be used to detect whether a string contains another string. In other words, whether a string is a substring of another string.
just compare the 2 with '==='. If the two strings are abc and cde , should they be considered "identical" because of c ? if you know what the substring is, you can perform indexOf on both the strings to check if the substring exists.
So it turns out that, in C, it's actually impossible to write a proper "utility" substring function, that doesn't do any dynamic memory allocation, and that doesn't modify the original string. All of this is a consequence of the fact that C does not have a first-class string type.
The C# Contains() method is used to return a value indicating whether the specified substring occurs within this string or not. If the specified substring is found in this string, it returns true otherwise false.
You haven't told us the minimum length of the matching string, so I'll assuming the minimum length is 1
.
Therefore, you can write:
String1.Any(c => String2.Contains(c))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With