मैं जाँचना चाहता हूँ कि क्या स्ट्रिंग स्ट्रिंग में, "a" या "b" या "c", C # में है। मैं उपयोग करने की तुलना में एक अच्छे समाधान की तलाश में हूं
if (s.contains("a")||s.contains("b")||s.contains("c"))
जवाबों:
यदि आप एकल पात्रों की तलाश में हैं, तो आप उपयोग कर सकते हैं String.IndexOfAny()।
यदि आप मनमाना तार चाहते हैं, तो मुझे "सीधे" प्राप्त करने के लिए एक .NET विधि के बारे में पता नहीं है, हालांकि एक नियमित अभिव्यक्ति काम करेगी।
खैर, यह हमेशा है:
public static bool ContainsAny(this string haystack, params string[] needles)
{
foreach (string needle in needles)
{
if (haystack.Contains(needle))
return true;
}
return false;
}
उपयोग:
bool anyLuck = s.ContainsAny("a", "b", "c");
||हालाँकि तुलना की अपनी श्रृंखला के प्रदर्शन से मेल नहीं खाता ।
public static bool ContainsAny(this string haystack, params string[] needles) { return needles.Any(haystack.Contains); }
यहाँ एक LINQ समाधान है जो वस्तुतः समान है लेकिन अधिक स्केलेबल है:
new[] { "a", "b", "c" }.Any(c => s.Contains(c))
var values = new [] {"abc", "def", "ghj"};
var str = "abcedasdkljre";
values.Any(str.Contains);
आप नियमित अभिव्यक्ति के साथ कोशिश कर सकते हैं
string s;
Regex r = new Regex ("a|b|c");
bool containsAny = r.IsMatch (s);
यदि आपको किसी विशिष्ट StringComparison(उदाहरण के लिए मामले को अनदेखा करने के लिए) ContainsAny की आवश्यकता है तो आप इस स्ट्रिंग एक्सटेंशन्स विधि का उपयोग कर सकते हैं।
public static class StringExtensions
{
public static bool ContainsAny(this string input, IEnumerable<string> containsKeywords, StringComparison comparisonType)
{
return containsKeywords.Any(keyword => input.IndexOf(keyword, comparisonType) >= 0);
}
}
के साथ उपयोग StringComparison.CurrentCultureIgnoreCase:
var input = "My STRING contains Many Substrings";
var substrings = new[] {"string", "many substrings", "not containing this string" };
input.ContainsAny(substrings, StringComparison.CurrentCultureIgnoreCase);
// The statement above returns true.
”xyz”.ContainsAny(substrings, StringComparison.CurrentCultureIgnoreCase);
// This statement returns false.
जैसा कि एक स्ट्रिंग वर्णों का एक संग्रह है, आप उन पर LINQ एक्सटेंशन विधियों का उपयोग कर सकते हैं:
if (s.Any(c => c == 'a' || c == 'b' || c == 'c')) ...
यह स्ट्रिंग को एक बार स्कैन करेगा और एक मैच मिलने तक प्रत्येक वर्ण के लिए एक बार स्ट्रिंग को स्कैन करने के बजाय, पहले रोके पर रोक देगा।
यह आपके द्वारा पसंद की जाने वाली किसी भी अभिव्यक्ति के लिए भी इस्तेमाल किया जा सकता है, उदाहरण के लिए वर्णों की एक श्रृंखला के लिए जाँच:
if (s.Any(c => c >= 'a' && c <= 'c')) ...
List<string> includedWords = new List<string>() { "a", "b", "c" };
bool string_contains_words = includedWords.Exists(o => s.Contains(o));
// Nice method's name, @Dan Tao
public static bool ContainsAny(this string value, params string[] params)
{
return params.Any(p => value.Compare(p) > 0);
// or
return params.Any(p => value.Contains(p));
}
Anyकिसी के लिए, Allहर के लिए
static void Main(string[] args)
{
string illegalCharacters = "!@#$%^&*()\\/{}|<>,.~`?"; //We'll call these the bad guys
string goodUserName = "John Wesson"; //This is a good guy. We know it. We can see it!
//But what if we want the program to make sure?
string badUserName = "*_Wesson*_John!?"; //We can see this has one of the bad guys. Underscores not restricted.
Console.WriteLine("goodUserName " + goodUserName +
(!HasWantedCharacters(goodUserName, illegalCharacters) ?
" contains no illegal characters and is valid" : //This line is the expected result
" contains one or more illegal characters and is invalid"));
string captured = "";
Console.WriteLine("badUserName " + badUserName +
(!HasWantedCharacters(badUserName, illegalCharacters, out captured) ?
" contains no illegal characters and is valid" :
//We can expect this line to print and show us the bad ones
" is invalid and contains the following illegal characters: " + captured));
}
//Takes a string to check for the presence of one or more of the wanted characters within a string
//As soon as one of the wanted characters is encountered, return true
//This is useful if a character is required, but NOT if a specific frequency is needed
//ie. you wouldn't use this to validate an email address
//but could use it to make sure a username is only alphanumeric
static bool HasWantedCharacters(string source, string wantedCharacters)
{
foreach(char s in source) //One by one, loop through the characters in source
{
foreach(char c in wantedCharacters) //One by one, loop through the wanted characters
{
if (c == s) //Is the current illegalChar here in the string?
return true;
}
}
return false;
}
//Overloaded version of HasWantedCharacters
//Checks to see if any one of the wantedCharacters is contained within the source string
//string source ~ String to test
//string wantedCharacters ~ string of characters to check for
static bool HasWantedCharacters(string source, string wantedCharacters, out string capturedCharacters)
{
capturedCharacters = ""; //Haven't found any wanted characters yet
foreach(char s in source)
{
foreach(char c in wantedCharacters) //Is the current illegalChar here in the string?
{
if(c == s)
{
if(!capturedCharacters.Contains(c.ToString()))
capturedCharacters += c.ToString(); //Send these characters to whoever's asking
}
}
}
if (capturedCharacters.Length > 0)
return true;
else
return false;
}
आप रेगुलर एक्सप्रेशन का उपयोग कर सकते हैं
if(System.Text.RegularExpressions.IsMatch("a|b|c"))
trieडेटा संरचना देखें।