Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C#
Tip/Trick

How to return the List of strings from a List where String start with specified characters

Rate me:
Please Sign up or sign in to vote.
1.00/5 (2 votes)
5 Nov 2012CPOL 15.3K   1   2

 

Introduction

Return the List of strings from a List  where String start with specified characters. 

Background 

I got a requirement in project like I have list of strings in that I have to returns the list where a string starts with specified characters. Then I wrote one small utility. This utility i am sharing now. 

Using the code 

Here is the small utility to filter the list where string start with specified string.

C#
public static List<string> StartsWithStringContains(List<string> targerList, string filterStr)
{
    var resultList = from resultValues in targerList
                     where
                         (resultValues.StartsWith(filterStr))
                     select
                         resultValues;

    return resultList.ToList();
}

 Inputs “ targerList”

- List of strings where you want search, filterStr– Filter you

are expecting start with string.
 
For 
Example,   
 

C#
/* Define Target list where you want to apply the filter */
          List<string> targerList = new List<string> { "test", "test1", "test3", "fff" };

          List<string> resultList = StartsWithStringContains(targerList, "ff"); /* This function gives a result list where string start with "ff" */

Similar implementation to search for end's with string

C#
public static List<string> EndsWithStringContains(List<string> targerList, string filterStr)
    {

        var resultList = from resultValues in targerList
                      where (resultValues.EndsWith(filterStr))
                      select resultValues;

        return resultList.ToList();

    }

Invoke it like bellow 

C#
/* Define Target list where you want to apply the filter */
          List<string> targerList = new List<string> { "test", "test1", "test3", "fff" };

          List<string> resultList = EndsWithStringContains(targerList, "ff"); /* This function gives a result list where string ends with "ff" */

 

Finally String matches in any position,

C#
public static List<string> StringContains(List<string> targerList, string filterStr)
       {
           var resultList = from resultValues in targerList
                            where
                                (resultValues.Contains(filterStr))
                            select
                                resultValues;

           return resultList.ToList();
       }
 

 


Complete

Example:
 

C++
 namespace StringLib
 
{
 
    class Program
 
    {
 
        static void Main(string[] args)
 
        {
 
            List<string> strList = new List<string> { "test", "test1", "test3", "fff" };
 
            strList = StartsWithStringContains(strList, "te");
 
            foreach (string s in strList)
 
            {
 
                Console.WriteLine(s);
 
            }
 
            Console.Read();
 
            
 
        }
 
  public static List<string> StartsWithStringContains(List<string> targerList, string filterStr)
        {
            var resultList = from resultValues in targerList
                             where
                                 (resultValues.StartsWith(filterStr))
                             select
                                 resultValues;

            return resultList.ToList();
        }

        public static List<string> EndsWithStringContains(List<string> targerList, string filterStr)
        {

            var resultList = from resultValues in targerList
                          where (resultValues.EndsWith(filterStr))
                          select resultValues;

            return resultList.ToList();


        }

        public static List<string> StringContains(List<string> targerList, string filterStr)
        {
            var resultList = from resultValues in targerList
                             where
                                 (resultValues.Contains(filterStr))
                             select
                                 resultValues;

            return resultList.ToList();
        }

    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Semantic Space Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General[My vote of 1] Just the obvious Linq statements Pin
Matt T Heffron6-Nov-12 8:28
professionalMatt T Heffron6-Nov-12 8:28 
This is just the fairly obvious Linq statement(s).
It is not a solution to anything that is so tricky or obscure that having found this "solution" is worth posting here.

Also, I see no reason to encapsulate them, the Linq reads just as well in-line.

Even still, if you are going to encapsulate them in extension methods, then the methods should return the simplest result type. In these cases, that would be IEnumerable<string>. It is likely that these would be used where enumerability is sufficient and creating a whole new List<string> is overkill.
Question[My vote of 1] Why wrapping? Pin
Pushparaj Adigopula5-Nov-12 23:05
Pushparaj Adigopula5-Nov-12 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.