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

本文关键字:单个 Qoute 替换 字符串 | 更新日期: 2023-09-27 17:59:13

假设,我有以下字符串:

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

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

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

您可以使用正则表达式来实现这一点:

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上进行拆分。然后,它将替换结果字符串数组中的每个单引号。最后,它将各部分重新连接在一起。

我明白了。你想避免逗号在一个引号内分开,对吧?

我想出了以下扩展方法:

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

这将导致:

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

您可能需要使用堆栈来检测要保留的逗号的单引号外壳。以下是代码片段:

        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;
                    }
                }
            }
        }
         string str = "'Hello, how are you?', 'your name', what you want";
         string str1=str.Replace("',","");
         str1 = str1.Replace("'", "");
         Console.WriteLine(str1);
         Console.ReadLine();

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

注意:如果我错了,请告诉我你想要的输出。我会告诉你如何得到你想要的。