在字符串中没有逗号的单个Qoute替换

时间:2016-07-29 06:11:24

标签: c# asp.net

假设我有以下字符串:

string str = "'Hello, how are you?', 'your name', what you want";

我可以用以下内容替换单引号和逗号:

str.Replace("'", "''");
string[] word = str.Split(',');

我想知道如何获得如下所示的输出:

Hello, how are you? your name what you want

单引号内的逗号不会被替换,只能在它之外。

4 个答案:

答案 0 :(得分:3)

您可以使用正则表达式完成此操作:

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);
}

代码使用表达式在单引号之外的splittingChar上拆分。然后它替换结果字符串数组中的每个单引号。最后,它将各部分重新组合在一起。

答案 1 :(得分:2)

我明白了。你想避免在单引号中分割逗号,对吧?

我想出了以下扩展方法:

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();
    }
}

现在,关于你的输出。您可以使用string.Join(string, string[])将字符串放在数组中:

string.Join("", word); // This puts all the strings together with "" (excactly nothing) between them

这将导致:

  你好,你好吗?你的名字你想要什么

答案 2 :(得分:2)

您可能希望使用堆栈来检测要保留的逗号的单引号机箱。以下是代码段:

        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;
                    }
                }
            }
        }

答案 3 :(得分:1)

         string str = "'Hello, how are you?', 'your name', what you want";
         string str1=str.Replace("',","");
         str1 = str1.Replace("'", "");
         Console.WriteLine(str1);
         Console.ReadLine();

输出: 你好,你好吗?你的名字你想要什么

注意:如果我错了,请告诉我你想要的输出。我会告诉你如何随心所欲。