How to search a Substring in String array in C#

前端 未结 4 654
予麋鹿
予麋鹿 2021-01-13 09:55

How to search for a Substring in String array? I need to search for a Substring in the string array. The string can be located in any part of the array (element) or within

4条回答
  •  走了就别回头了
    2021-01-13 10:21

    To find the substrings in the String array using C#

        List searchitem = new List();
        string[] arrayStrings = {
           "Welcome to SanJose",
           "Welcome to San Fancisco","Welcome to New York",
           "Welcome to Orlando", "Welcome to San Martin",
           "This string has Welcome to San in the middle of it"
        };
       string searchkey = "Welcome to San";
       for (int i = 0; i < arrayStrings.Length; i++)
       {
        if (arrayStrings[i].Contains(searchkey))//checking whether the searchkey contains in the string array
        {
         searchitem.Add(arrayStrings[i]);//adding the matching item to the list 
        }
       string searchresult = string.Join(Environment.NewLine, searchitem);
    

    Output for Searchresult:

    Welcome to SanJose

    Welcome to San Fancisco

    Welcome to San Martin

    This string has Welcome to San in the middle of it

提交回复
热议问题