Method ExtString.Like (+ 2 overloads)
Overload
Compares this string with a string that possibly contains wildcard characters.
Returns true
if the strings match.
public static bool Like(this string t, string pattern, bool ignoreCase = false)
Parameters
t (string)
This string. If |
pattern (string)
String that possibly contains wildcard characters. Cannot be |
ignoreCase (bool)
Case-insensitive. |
Returns
Exceptions
ArgumentNullException
pattern is |
Remarks
Wildcard characters:
Character | Will match | Examples |
---|---|---|
* |
Zero or more of any characters. | "start*" , "*end" , "*middle*" |
? |
Any single character. | "date ????-??-??" |
There are no escape sequences for *
and ?
characters.
Uses ordinal comparison, ie does not depend on current culture.
See also: wildcard expression.
Examples
string s = @"C:\abc\mno.xyz";
if(s.Like(@"C:\abc\mno.xyz")) print.it("matches whole text (no wildcard characters)");
if(s.Like(@"C:\abc\*")) print.it("starts with");
if(s.Like(@"*.xyz")) print.it("ends with");
if(s.Like(@"*mno*")) print.it("contains");
if(s.Like(@"C:\*.xyz")) print.it("starts and ends with");
if(s.Like(@"?:*")) print.it("any character, : and possibly more text");
See Also
Overload(next)
Compares this string with a string that possibly contains wildcard characters.
Returns true
if the strings match.
public static bool Like(this ReadOnlySpan<char> t, string pattern, bool ignoreCase = false)
Parameters
t (ReadOnlySpan<char>)
This string. If |
pattern (string)
String that possibly contains wildcard characters. Cannot be |
ignoreCase (bool)
Case-insensitive. |
Returns
Exceptions
ArgumentNullException
pattern is |
Overload(top)
Calls Like(string, string, bool) for each wildcard pattern specified in the argument list until it returns true
.
Returns 1-based index of the matching pattern, or 0 if none.
public static int Like(this string t, bool ignoreCase = false, params string[] patterns)
Parameters
t (string) |
ignoreCase (bool)
Case-insensitive. |
patterns (string[])
One or more wildcard strings. The array and strings cannot be |
Returns
Exceptions
ArgumentNullException
A string in patterns is |