0

In my project I'm having a StringBuilder which takes the selected value of dropdown lists.

StringBuilder builder = new StringBuilder();
builder.Append(ddl_model.SelectedValue);
builder.Append(ddl_language.SelectedValue);

foreach (string str in list)
{
    if (str.Contains(builder.ToString()))
    {
        lstpdfList.Items.Add(str);
    }
}

It works with one value. I would like to make that I can check if contains two or more words.

I have a file like PM12_Manual_Rev1_EN . Now I can find if it contains PM12. But there is a lot of them. So I would like to check if contains PM12 + EN.

Florent
  • 12,310
  • 10
  • 49
  • 58
N K
  • 287
  • 3
  • 11
  • 19
  • What is list here ? and which type of info it contain? – Sunny Sep 05 '12 at 08:50
  • Have you tried regular expressions? or even just 2 different .Contains if regex is too much for you? – JTMon Sep 05 '12 at 08:50
  • @Sunny "list" is a List. Contains Informations like I mentioned : "PM12_Manual_Rev1_EN" for example. I would like to check if contains more keyword for example PM + EN or so. Thank you ! – N K Sep 05 '12 at 08:53
  • Can't you just use it like `((str.Contains("PM12") && str.Contains("EN"))` ? – Shakti Prakash Singh Sep 05 '12 at 08:54
  • thanks for reply so it means list structure is - list[0] =PM12,list[1]=Manual,list[2]Rev1,list[3]=EN.and selected items may contain more than one item out of that? – Sunny Sep 05 '12 at 08:57
  • Well, now I see what you are asking. Your question isn't very clear. You are appending both PM12 and EN in the same StringBuilder and want to search if both these strings are present in str. Well, I don't think your approach is correct. You are appending 2 string and making it one in the string builder. Why can't you keep both model and language separate and search separately? – Shakti Prakash Singh Sep 05 '12 at 09:01
  • Better to use .EndsWith("EN") instead of contains, because you know that the string is in the end, and hopefully, "EN" isn't part of document name – Anton Sep 05 '12 at 09:04

3 Answers3

4

1) Don't call builder.ToString() inside the foreach, it will rebuild the string everytime, and it defeats the performance purpose of StringBuilder.

2) Don't use a StringBuilder, use a List in which you store the words you want to match for, if each selected value may contain several words, split them:

var keywords = new List<string>();
keywords.AddRange(ddl_model.SelectedValue.Split(' '));
keywords.AddRange(ddl_language.SelectedValue.Split(' '));

foreach(string str in list)
   if (keywords.Any(keyword => str.Contains(keyword))
      lstpdfList.Items.Add(str);
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
0

You could use regexes.

    string testString ="PM12_Manual_Rev1_EN";       

    var wordRegex = new Regex( "PM12.*EN", RegexOptions.IgnoreCase );

    if (wordRegex.IsMatch( testString ))
    {
        Console.WriteLine("we've found multiple matches, REGEX edition");       
    }

Or you could use Contains with Linq like this:

    string testString ="PM12_Manual_Rev1_EN";

    var checkWords = new List<String>() {"PM12", "EN"};
    if(checkWords.All(w => testString.Contains(w)))
    {
        Console.WriteLine("we've found multiple matches");      
    }
Community
  • 1
  • 1
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
0

You should use String.Contains Method, its the easiest way! It returns a value indicating whether the specified String object occurs within this string.

@Shaks has right.

Sylca
  • 2,523
  • 4
  • 31
  • 51