Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
First of all, I'm very sorry for my bad English.

Is there's any way to parse arguments from string?
I'll explain my question to showing an example.

VB
'String Input: This is "TEST STRING"
Array[0] = "This"
Array[1] = "is"
Array[2] = "TEST STRING"

'String Input: Another Test String, Separate Strings with "space character"
Array[0] = "Another"
Array[1] = "Test"
Array[2] = "String,"
Array[3] = "Separate"
Array[4] = "Strings"
Array[5] = "with"
Array[6] = "space character"

'String Input: "This " is "also another" "test " "string  " "!"
Array[0] = "This "
Array[1] = "is"
Array[2] = "also another"
Array[3] = "test "
Array[4] = "string  "
array[5] = "!"


If you know this, please let me know
I'm sorry again for my bad english. Thanks!
Posted
Updated 29-Jun-13 20:42pm
v3
Comments
Akinmade Bond 30-Jun-13 2:38am    
From the looks of things. You want to split a sentence with spaces into words leaving the ones with a quote intact right?
SlaneR 30-Jun-13 2:44am    
I didn't completely understand your sentence but I guess it's right
SlaneR 30-Jun-13 2:43am    
to build my custom console :D
Akinmade Bond 30-Jun-13 5:01am    
See my answer.

VB
Dim myString As String = "Another Test String, Separate Strings with ""space character"""

'Everything here should be on the same line.
Dim result = myString.Split(""""c).[Select](Function(element, index) If(index Mod 2 = 0, element.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries), New String() {element})).SelectMany(Function(element) element).ToList()


Or use this if you still want to keep the quotes.
You will have to Import System.Text.RegularExpressions

VB
'Everthing goes on the same line.
Dim parts = Regex.Matches(myString, "[\""].+?[\""]|[^ ]+").Cast(Of Match)().[Select](Function(m) m.Value).ToList()
 
Share this answer
 
v2
Below is sample for the same.

string input = "one \"two two\" three \"four four\" five six";
          var result = input.Split('"')
                   .Select((element, index) => index % 2 == 0  // If even index
                                         ? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)  // Split the item
                                         : new string[] { element })  // Keep the entire item
                   .SelectMany(element => element).ToList();


You can also check link below

Split a string that has white spaces, unless they are enclosed within “quotes”?[^]
 
Share this answer
 
Comments
SlaneR 30-Jun-13 3:49am    
How do I change this code??
In VB.NET, Select method only takes 1 argument...

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