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

e.g:
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
C#
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?

thanks
Posted
Comments
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? :-)
--SA
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.

—SA
 
Share this answer
 
Comments
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.
--SA
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.
--SA
This probably isn't the most efficient way, but this should be pretty close to what you need:

C#
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));
			stringList.Add(sb.ToString());
		}
		else
		{
			if (inQuotes)
			{
				sb.Append(s);
				sb.Append(" ");
			}
			else
			{
				stringList.Add(s);
			}
		}
	}

	return stringList;
}
 
Share this answer
 
v2
Comments
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: http://stackoverflow.com/questions/554013/regular-expression-to-split-on-spaces-unless-in-quotes[^]
 
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

C#
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[]{' '},
                StringSplitOptions.RemoveEmptyEntries));
        else if (temp[i].Trim() != string.Empty)
            elements.Add(temp[i].Trim());
}
//elements
//
//This
//is
//my string
//for
//searching in my
//program
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-May-12 22:43pm    
A 5.
--SA
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