【问题标题】:Add spaces in string but not between quotes在字符串中添加空格,但不在引号之间
【发布时间】:2014-01-08 21:09:08
【问题描述】:

我有这个字符串:

string myString = "do Output.printString(\"Do you want to Hit (h) or Stand (s)?\");";

我的字符串是纯文本:

do Output.printString("你想击中(h)还是站立(s)?");

我想做:

做输出。 printString("Do@you@want@to Hit@(h)@or@Stand@(s)?");

这个想法是每个单词之间有一个空格,但是如果撇号中有一个字符串,我希望它是 WITHOUT SPACE 并且在这个函数之后我可以这样做:

s.Split(' ');

并在一个字符串中获取字符串。

我所做的是:

 public static string PrepareForSplit(this string s)
    {
        string ret = "";
        if (s.Contains("\""))
        {
            bool equalsAppear = false;
            foreach (var nextChar in s)
            {
                char charToConcat;
                if (nextChar == '"')
                {
                   equalsAppear= equalsAppear == true ? false : true;
                }

                if (nextChar == ' ' && equalsAppear)
                {

                    charToConcat = '@';

                }
                else
                {
                    charToConcat = nextChar;
                }
                ret += charToConcat;
            }

        }

        if (String.IsNullOrWhiteSpace(ret))
            ret = s;
        string[] symbols = {"{", "}", "(", ")", "[", "]", ".",
    ",", ";", "+", "-", "*", "/", "&", "|", "<", ">", "=", "~","#"};

        foreach(var symbol in symbols)
        if(ret.Contains(symbol))
        {
            if (!ret.Contains('"') || !((symbol=="-") || symbol==","))
            {
                ret = ret.Replace(symbol, " " + symbol + " ");
            }
        }
        if(ret.Contains("\t"))
        {
            ret = Regex.Replace(ret, @"\t", " ");
        }

        return ret;

    }

我的问题是在这个函数的末尾我得到了这个字符串:

做输出。 printString("Do@you@want@to@Hit@(h)@or@Stand@(s)?");

正如您在假设没有空格的字符串中看到的那样,我有空格,然后我的程序无法正常运行。有人请帮忙!

【问题讨论】:

  • 你可以用正则表达式做到这一点。
  • 怎么样?你会写正则表达式吗?
  • printString 如何打印报价单?
  • printstring 是我的字符串的一部分我会更新这个问题,它会更清楚......
  • 当您说“撇号”(')时,您的意思是“括号”(())吗?

标签: c# string c#-4.0 tokenize


【解决方案1】:

我会使用正则表达式来提取您的字符串。

您可能会像这样输入起始字符串:

string source = "do Output.printString(\"Do you want to Hit (h) or Stand (s)?\");";

试试这个正则表达式:

\("([^\"]+)

圆括号之间的组(即捕获组)就是您要查找的。​​p>

编辑:像这样使用它(基于http://www.dotnetperls.com/regex-match

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        // First we see the input string.
        string source = "do Output.printString(\"Do you want to Hit (h) or Stand (s)?\");";

        // Here we call Regex.Match.
        Match match = Regex.Match(source, "\\(\"([^\"]+)");

        // Here we check the Match instance.
        if (match.Success)
        {
            // Finally, we get the Group value and display it.
            string key = match.Groups[1].Value;
            Console.WriteLine("result: "+ key);
        }
        else
        {
            Console.WriteLine("nothing found");
        }
        Console.ReadLine();
    }
}

Edit2:现在可以了 :-)

【讨论】:

  • 能写的更清楚点吗?看不懂怎么用
【解决方案2】:

我建议您在撇号处拆分整个字符串。这将使区分撇号内的部分和其他部分变得更加容易。

string[] parts = s.Split('"');

现在你有:

part[0] ==> "do Output . printString ("
part[1] ==> "Do@you@want@to Hit@(h)@or@Stand@(s)?"
part[2] ==> ");"

即,part[] 中的偶数索引在撇号之外,奇数索引在撇号之内。

// Treat the parts not between apostrophes:
for (int i = 0; i < parts.Length; i += 2) {
    part[i] = InsertSpacesBetweenWords(part[i]); 
}

string result = String.Join("\"", part);

顺便说一句:在你的例子中,你可以简化

equalsAppear = equalsAppear == true ? false : true;

equalsAppear = !equalsAppear;

通过使用逻辑非运算符!

【讨论】:

  • 非常好的答案,这就是我喜欢堆栈溢出的原因,谢谢!这解决了我的问题并节省了我很多时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-24
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
相关资源
最近更新 更多