Click here to Skip to main content
15,848,451 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i like to split a string for search operation to a stringList

Textbox input
This is "my string" for "searching in my" program

StringList elements
1. This
2. is
3. my string
4. for
5. searching in my
6. program

For splitting all in separate words i can use
SearchString.Split(' ').ToList();

but then i don't have the combined search tags as one entry in my stringList

does someone have a solution for me?

Sergey Alexandrovich Kryukov 22-May-12 12:54pm    
What is the criterion for splitting, say, "this" and "is" but putting together "my string" and "searching in my"? Quotation marks? :-)
Member 7959769 22-May-12 13:12pm    
The seachstring should be splitted after every ' ' but words inside '"' should stay together.

The Input: I am learning to split strings
should result: [[I],[am],[learning],[to],[split],[strings]]

The Input: I am "learning to split" strings
should result: [[I],[am],[learning to split],[strings]]

The Input: "I am" "learning to split" strings
should result: [[I am],[learning to split],[strings]]

The Input: "I am learning to split strings"
should result: [[I am learning to split strings]]
VJ Reddy 22-May-12 20:44pm    
I have added a solution which checks No. of double quotes, removes empty entries and trims. Please see.

I have something similar in my article Enumeration-based Command Line Utility[^].

For testing of this utility, I created a simulator of the pre-parser .NET uses to parse command line strings the way you show. Please see the chapter "6. CommandLine Testing" and locate the class SA.Universal.Utilities.CommandLineSimulationUtility — it does — as far as I could understand you — what you want.

Share this answer
Member 7959769 22-May-12 13:27pm    
Thanks for you help, as i saw in the method header of
"internal class CommandLineTokenizer "
i am sure your solutin will work too, but lewax00 solution looks much easier and is more than good enought for my searching programm.
Sergey Alexandrovich Kryukov 22-May-12 14:55pm    
Maybe. It looks pretty close.
VJ Reddy 22-May-12 20:43pm    
Seen the article. It is very good and flexible. 5!
I have added a solution for this particular case.
Sergey Alexandrovich Kryukov 22-May-12 22:44pm    
Thank you, VJ.
This probably isn't the most efficient way, but this should be pretty close to what you need:

public static ICollection<string> ConvertToStringList(string input)
	string[] split = input.Split(' ');

	List<string> stringList = new List<string>();

	bool inQuotes = false;
	StringBuilder sb = new StringBuilder();

	foreach (string s in split)
		if (s.StartsWith("\""))
			inQuotes = true;
			sb = new StringBuilder(s.Substring(1));
			sb.Append(" ");
		else if (s.EndsWith("\""))
			inQuotes = false;
			sb.Append(s.Substring(0, s.Length - 1));
			if (inQuotes)
				sb.Append(" ");

	return stringList;
Share this answer
Member 7959769 22-May-12 13:24pm    
Thanks for your help!!
VJ Reddy 22-May-12 20:43pm    
Good answer. 5!
This is probably about as good as you will get. It uses regular expression to split:[^]
Share this answer
As an alternative, the following code can be used to split the string into separate words.

It first checks for even number of double quotes within the string.
It removes the empty entries generated by split.
It trims the words so that there is no extra space at the beginning and end of the word

string input = @"  This is   "" my string "" for ""searching in my ""  program";
//string input = "I am learning to split strings";
//string input =  @"  I am ""  learning to split ""    strings";
//string input = @"""I am  "" ""learning to split""    strings" ;
//string input = @"""      I am learning to split strings""";
if (input.Count (ch => ch == '"') % 2 != 0)
    Console.WriteLine ("Inadequate double quotes");
else {
    List<string> elements = new List<string>();
    string[] temp = input.Split('"');
    for(int i=0; i<temp.Length; i++)
        if (i%2==0)
            elements.AddRange(temp[i].Split(new char[]{' '},
        else if (temp[i].Trim() != string.Empty)
//my string
//searching in my
Share this answer
Sergey Alexandrovich Kryukov 22-May-12 22:43pm    
A 5.
VJ Reddy 22-May-12 22:58pm    
Thank you, SA :)

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900