1

I want the user to insert a string as a parameter, and afterwards check if that string is equal to one of several strings.

The way I do it now is

....
if(stringParam != "text1" || stringParam != "text2" || stringParam != "text3"...  stringParam != "text10")
....

is there not a way that to write this more readable / pretty? There probably is, but I couldn't figure a way.

Also it doesn't matter if the strings are uppercase or lowercase.

nelion
  • 1,712
  • 4
  • 17
  • 37
  • _"one of several strings"_, are these strings have kind of naming pattern like in your example that contains numeric data? – CodeRed Sep 24 '19 at 06:03

3 Answers3

4

You can use a Hashtable, Dictionary or HashSet. You can store in it the strings as keys and then use the method ContainsKey()/Contains() to see if your stringParam matches any of the keys stored previously ("text1", "text2" and so on).

    HashSet<string> mySet = new HashSet<string>(); 


    mySet.Add("text1"); 
    mySet.Add("text2"); 
    mySet.Add("text3"); 
    mySet.Add("text4"); 

    if (mySet.Contains(stringParam)) 
        Console.WriteLine("It matched"); 
Alin Ungureanu
  • 211
  • 1
  • 8
  • 3
    Some of the constructors of a `HashSet` accept an `IEqualityComparer`. So to accomodate for "it doesn't matter if the strings are uppercase or lowercase", One could use one of the "IgnoreCase" string comparers like for example: `new HashSet(StringComparer.CurrentCultureIgnoreCase)`. – Corak Sep 24 '19 at 06:27
0

You could use regex matching with a single alternation:

Regex regex = new Regex(@"\b(?:text1|text2|text3|...|text10)\b");
string stringParam = "passes";
Match match = regex.Match(stringParam);
if (!match.Success)
{
    Console.WriteLine("MATCH");
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You could try this:

// build a string array containing what you want to match.
var myStrings = new [] { "TEXT1", "TEXT2", "TEXT3", ....... };

// search in it
if (myStrings.Contains(stringParam.ToUpper()))
{
    // yes
}

You could also use a HashSet<T>.

But this 'smells' like you're doing something which could be done other ways more efficient, but there is a lack of information for us.


Like Fildor said, you might want to use this one.

// build a string array containing what you want to match.
var myStrings = new [] { "TEXT1", "TEXT2", "TEXT3", ....... };

// search in it
if (myStrings.Any(s => String.Compare(s, stringParam, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace))
{
    // yes
}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • Just a sidenote because I ran into it: `toUpper` doesn't work with turquish "i". Because the Uppercase turquish "i" is not what you get from `toUpper`... – Fildor Sep 24 '19 at 06:10
  • You probably need to use the `myStrings.Any(s => String.Compare(s, stringParam,, CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace))` – Jeroen van Langen Sep 24 '19 at 10:05
  • Exactly, just wanted to leave that remark for people trying to use your solution. – Fildor Sep 24 '19 at 11:36