Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I would like to separate a line into two strings, one key and one value.
I can read the line
CONNECTION.TEST "the connections is confirmed"

and would like to separate into
CONNECTION.TEST
and
"the connections is confirmed"
.
The space between word can either be a tab. or a space.

Thanks in advance
Merh

What I have tried:

 string line = reader.ReadLine();
char[] delimiterChars = {'\t " "' };
while (line != null)
{
      string[] words = line.Split(delimiterChars);

...
Posted
Updated 2-Dec-20 11:08am
Comments
CHill60 2-Dec-20 13:10pm    
Is "the connections is confirmed" actually surrounded by those quotes in line? If so then you should use the double quote as the delimiter on the split
Maciej Los 2-Dec-20 13:27pm    
You have to find first occurance of space of tab. Then split string on it.
PIEBALDconsult 2-Dec-20 23:50pm    
Regular Expressions.

C#
var first = line;
string second = null;
var i = line.IndexOfAny(new char[] {' ','\t'});

if(-1<i) // found a space or tab
{
   first = line.Substring(0,i).TrimEnd();
   second = line.Substring(i+1).TrimStart();
}


Something like that. I'm eating so I haven't compiled or ran it yet, but I waved a dead chicken around earlier today so you should be fine.
 
Share this answer
 
Comments
BillWoodruff 3-Dec-20 1:27am    
+5
Quote:
C#
string[] words = line.Split(delimiterChars);

split is wrong approach because there is more than 1 space in line.
I recommend to use RegEx because you will be able to describe a sequence that will match only once.
This will split at space only id followed by double quote:
(\t| )(?=")


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Comments
BillWoodruff 3-Dec-20 1:28am    
+5
Patrice T 3-Dec-20 2:59am    
Thank you
Richard Deeming 3-Dec-20 5:38am    
Assuming there's never a separator in the key, you could still use Split by specifying the maximum number of elements to return.
string input = "one two three";
input.Split(new[] { ' ' }) == { "one", "two", "three" }
input.Split(new[] { ' ' }, 2) == { "one", "two three" }
Patrice T 3-Dec-20 5:50am    
Good point, you should make it a solution.
Richard Deeming 3-Dec-20 5:56am    
The IndexOf option would still be simpler, and your Regex option would be more robust. :)
C#
// Linq not used here

string line = @"CONNECTION.TEST ""the connections is confirmed""";

// find first whitespace char ndx
int ndx = Array.FindIndex(line.ToCharArray(), ch => char.IsWhiteSpace(ch));

string line1 = line.Substring(0, ndx);

// using Substring
string line2BySubString = line.Substring(ndx).TrimStart();

// using Remove
string line2ByRemove = line.Remove(0,ndx).TrimStart();
Some discussion here of various techniques for finding the first index of a specific character in a string: [^]
 
Share this answer
 
v3
Comments
Patrice T 3-Dec-20 3:10am    
5

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