Reputation: 3149
Suppose, I've the following string:
string str = "'Hello, how are you?', 'your name', what you want";
I can replace the single quote and comma with the followings:
str.Replace("'", "''");
string[] word = str.Split(',');
I was wondering how could I get the output like the below:
Hello, how are you? your name what you want
The comma inside the single quote will not be replaced, only outside of it.
Upvotes: 4
Views: 173
Reputation: 804
I see. You want to avoid the splitting of the commas inside a single quote, right?
I came up with the following extension method:
static class Extention
{
public static string[] SplitOutsideSingleQuotes(this string text, char splittingChar)
{
bool insideSingleQuotes = false;
List<string> parts = new List<string>() { "" }; // The part in which the text is split
foreach (char ch in text)
{
if (ch == '\'') // Determine whenever we enter or exit a single quote
{
insideSingleQuotes = !insideSingleQuotes;
continue; // The single quote shall not be in the final output. Therefore continue
}
if (ch == splittingChar && !insideSingleQuotes)
{
parts.Add(""); // There is a 'splittingChar'! Create new part
}
else
parts[parts.Count - 1] += ch; // Add the current char to the latest part
}
return parts.ToArray();
}
}
Now, as for your output. You can use string.Join(string, string[])
to put the strings in the array together:
string.Join("", word); // This puts all the strings together with "" (excactly nothing) between them
This will result in:
Hello, how are you? your name what you want
Upvotes: 2
Reputation: 157
string str = "'Hello, how are you?', 'your name', what you want";
string str1=str.Replace("',","");
str1 = str1.Replace("'", "");
Console.WriteLine(str1);
Console.ReadLine();
Output: Hello, how are you? your name what you want
Note: If am I wrong means please tell me the output you want. I will tell you how to get as you want.
Upvotes: 1
Reputation: 25897
You might want to use a stack to detect the single quote enclosure for the comma you want to preserve. Here is the code snippet:
static void Main(string[] args)
{
string str = "'Hello, how are you?', 'your name', what you want";
string outputString = String.Empty;
Stack<char> runningStack = new Stack<char>();
foreach (var currentCharacter in str)
{
if (currentCharacter == '\'')
{
if (runningStack.Count > 0)
{
//this is closing single quote. so empty the stack
runningStack.Clear();
}
else
{
runningStack.Push(currentCharacter);
}
}
else
{
if (currentCharacter == ',')
{
if (runningStack.Count > 0)
{//there was an opening single quote before it. So preserve it.
outputString += currentCharacter;
}
}
else
{
outputString += currentCharacter;
}
}
}
}
Upvotes: 2
Reputation: 6744
You can accomplish this using Regular Expressions:
private const string SPLIT_FORMAT = "{0}(?=(?:[^']*'[^']*')*[^']*$)";
public static string SplitOutsideSingleQuotes(this string text, char splittingChar)
{
string[] parts = Regex.Split(text, String.Format(SPLIT_FORMAT, splittingChar), RegexOptions.None);
for (int i = 0; i < parts.Length; i++)
{
parts[i] = parts[i].Replace("'", "");
}
return String.Join("", parts);
}
The code uses the expression to split on the splittingChar
outside of single quotes. It then replaces each single quote in the resultant string array. Lastly it joins the parts back together.
Upvotes: 3