【发布时间】: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 是我的字符串的一部分我会更新这个问题,它会更清楚......
-
当您说“撇号”(
')时,您的意思是“括号”(())吗?