Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 5 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.

 

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.

        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,   
 

  /* 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

    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 

  /* 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,

 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:
 

 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)

Share

About the Author

santhosh-padamatinti
Software Developer Semantic Space Technologies
India India
No Biography provided
Follow on   Twitter

Comments and Discussions

 
General[My vote of 1] Just the obvious Linq statements PinmemberMatt T Heffron6-Nov-12 9:28 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 5 Nov 2012
Article Copyright 2012 by santhosh-padamatinti
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid