Double Quote and Comma in String

  • Thread starter Thread starter Ahmad A. Rahman
  • Start date Start date
A

Ahmad A. Rahman

Hi All,

I have a string as [1,"two"," three", "fo""ur", "fi,ve"] and I want to
split the string, which will turn into an array of:
Arr[0] = "1";
Arr[1] = "two";
Arr[2] = "three";
Arr[3] = "fo""ur";
Arr[4] = "fi,ve";

This has to be possible, but I really don't know the logic. Please help.

Thank you.

*- character "[" and "]" denotes the start and end of string.
 
Hi,

Doesn't the String.Split method with comma char specified as a separator do
the job?
 
It does the job but notice the last string - "fi,ve". Split method will
split that string into 2. "fi" and "ve".

My point here is to not split the string in between the starting "
charachter and the closing " until it reached a comma.

Another issue is to include the " character itself, like in a string:
string s = "6,\"as\"\"a;s,d\",\"normal string\"";

Typical split will split the string into {"6", "\"as\"\"a;s", "d\"",
"\"normal string\""};
I wish I can split it into {"6", "\"as\"\"a;s,d\"", "\"normal string\""}

I hope you can see my point.

Please help.

p/s: refer to MSSQL DTS Import/Export Wizard. It does the job so well.


Dmitriy Lapshin said:
Hi,

Doesn't the String.Split method with comma char specified as a separator do
the job?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ahmad A. Rahman said:
Hi All,

I have a string as [1,"two"," three", "fo""ur", "fi,ve"] and I want to
split the string, which will turn into an array of:
Arr[0] = "1";
Arr[1] = "two";
Arr[2] = "three";
Arr[3] = "fo""ur";
Arr[4] = "fi,ve";

This has to be possible, but I really don't know the logic. Please help.

Thank you.

*- character "[" and "]" denotes the start and end of string.
 
In this case, you probably will have to write your own splitter - and it's
pretty simple. Walk throught the string appending to another string as you
walk. When you encounter a quote, set a flag and continue walking. If you
encounter another quote reset the flag. If you encounter a comma when the
flag is set, add it to the string, if not, complete that string and move
over.

vJ
 
Ahmad,

Why don't you use regular expressions?

string expression = "\"1\",\"two\",\" three\", \"fo\"\"ur\", \"fi,ve\"";
MatchCollection col = Regex.Matches(expression,
@"(?:^|\s*\,\s*)(?:""(?<SubString>(?:""""|[^""])*)"")+",RegexOptions.IgnoreC
ase | RegexOptions.IgnorePatternWhitespace);
foreach (System.Text.RegularExpressions.Match match in col)
{
string subString = match.Groups["SubString"].Value;
}

dont forget to use namespace System.Text.RegularExpressions

B.T.W - it doesnt work if the first substring (1) is not surrounded with "
as well...

Picho
 
Looks like I need to master regular expression to do this.

I'll try to construct a better and robust reg exp and I'll come back later
for the solution. (in case somebody else stumbled on the same problem)

Thanks everybody.

Picho said:
Ahmad,

Why don't you use regular expressions?

string expression = "\"1\",\"two\",\" three\", \"fo\"\"ur\", \"fi,ve\"";
MatchCollection col = Regex.Matches(expression,
ase | RegexOptions.IgnorePatternWhitespace);
foreach (System.Text.RegularExpressions.Match match in col)
{
string subString = match.Groups["SubString"].Value;
}

dont forget to use namespace System.Text.RegularExpressions

B.T.W - it doesnt work if the first substring (1) is not surrounded with "
as well...

Picho


Ahmad A. Rahman said:
Hi All,

I have a string as [1,"two"," three", "fo""ur", "fi,ve"] and I want to
split the string, which will turn into an array of:
Arr[0] = "1";
Arr[1] = "two";
Arr[2] = "three";
Arr[3] = "fo""ur";
Arr[4] = "fi,ve";

This has to be possible, but I really don't know the logic. Please help.

Thank you.

*- character "[" and "]" denotes the start and end of string.
 
Sorry I really didn't notice the "fi,ve" string in the first place. In this
case, Regular Expressions are the way to go, as other people have advised.
They are not so complex to be mastered, and they are really powerful!

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ahmad A. Rahman said:
It does the job but notice the last string - "fi,ve". Split method will
split that string into 2. "fi" and "ve".

My point here is to not split the string in between the starting "
charachter and the closing " until it reached a comma.

Another issue is to include the " character itself, like in a string:
string s = "6,\"as\"\"a;s,d\",\"normal string\"";

Typical split will split the string into {"6", "\"as\"\"a;s", "d\"",
"\"normal string\""};
I wish I can split it into {"6", "\"as\"\"a;s,d\"", "\"normal string\""}

I hope you can see my point.

Please help.

p/s: refer to MSSQL DTS Import/Export Wizard. It does the job so well.


Dmitriy Lapshin said:
Hi,

Doesn't the String.Split method with comma char specified as a separator do
the job?

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Ahmad A. Rahman said:
Hi All,

I have a string as [1,"two"," three", "fo""ur", "fi,ve"] and I want to
split the string, which will turn into an array of:
Arr[0] = "1";
Arr[1] = "two";
Arr[2] = "three";
Arr[3] = "fo""ur";
Arr[4] = "fi,ve";

This has to be possible, but I really don't know the logic. Please help.

Thank you.

*- character "[" and "]" denotes the start and end of string.
 
Back
Top