在字符串中添加空格但不在引号之间添加空格

时间:2014-01-08 21:09:08

标签: c# string c#-4.0 tokenize

我有这个字符串:

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

我的字符串为纯文字:

  

做Output.printString("你想要击中(h)还是看台?");

我想成功:

  

做输出。 printString(" Do @ you @ want @ to Hit @(h)@或@ 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(&#34; Do @ you @ want @ to @ Hit @(h)@ or @ Stand @(s)?&#34;);

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

2 个答案:

答案 0 :(得分:2)

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

你可能会输入这样的起始字符串:

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

试试这个正则表达式:

\("([^\"]+)

圆括号(即捕获组)之间的组是您正在寻找的。

编辑:像这样使用它(基于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:现在可行: - )

答案 1 :(得分:1)

我建议你把整个字符串分成撇号。这样可以更容易区分撇号和其他部分。

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;

使用逻辑NOT运算符!