用双引号括住每个单词

时间:2014-03-20 07:51:57

标签: c# .net string

我必须在C#中编写函数,它将用双引号括住每个单词。我希望它看起来像这样:

"Its" "Suposed" "To" "Be" "Like" "This"

这是我到目前为止提出的代码,但它不起作用:

protected void btnSend_Click(object sender, EventArgs e)
{
    string[] words = txtText.Text.Split(' ');
    foreach (string word in words)
    {
        string test = word.Replace(word, '"' + word + '"');

    }
    lblText.Text = words.ToString();
}

8 个答案:

答案 0 :(得分:8)

嗯,这在某种程度上取决于你认为的单词',但你可以使用正则表达式:

lblText.Text = Regex.Replace(lblText.Text, @"\w+", "\"$0\"")

这将匹配字符串中的一个或多个'word' characters(在正则表达式的上下文中包括字母,数字和下划线)的任何序列,并用双引号将其包装。

要包装non-whitespace characters的任何序列,您可以使用\S代替\w

lblText.Text = Regex.Replace(lblText.Text, @"\S+", "\"$0\"")

答案 1 :(得分:3)

它不起作用,因为Replace不会改变它的论点。从不使用包含所需值的变量test

您也可以使用Linq进行以下操作:

return String.Join(" ", txtTextText.Split(' ').Select( word => "\"" + word "\""));

答案 2 :(得分:3)

变化:

protected void btnSend_Click(object sender, EventArgs e)
{
    string[] words = txtText.Text.Split(' ');
    foreach (string word in words)
    {
        string test = word.Replace(word, '"' + word + '"');
    }
    lblText.Text = words.ToString();
}

要:

protected void btnSend_Click(object sender, EventArgs e)
{
    string[] words = txtText.Text.Split(' ');
    string test = String.Empty;
    foreach (string word in words)
    {
        test += word.Replace(word, '"' + word + '"');
    }
    lblText.Text = test.ToString();
}

你没有对test字符串做任何事情。并且字符串在每次循环迭代时都被重置,现在它正在追加。

答案 3 :(得分:3)

坚持OPs方法:

string input ="It's going to be a fine day";
string[] words = input.Split(' ');
string result = String.Empty;
foreach (string word in words)
{
    result += String.Format("\"" + word + "\" ");
}

我在单词后添加了一个空格。

因此您的代码应如下所示:

protected void btnSend_Click(object sender, EventArgs e)
{
    string test = String.Empty;
    string[] words = txtText.Text.Split(' ');
    foreach (string word in words)
    {
        test += String.Format("\"" + word + "\" ");
    }
    lblText.Text = test;
}

答案 4 :(得分:2)

您可以在此处使用 Linq

 lblText.Text = String.Join(" ", txtText.Text.Split(' ').Select(x => "\"" + x + "\""));

答案 5 :(得分:1)

如果您需要为每个单词添加单引号,请尝试以下操作:

List<string> list = new List<string>();
int i = 0;
yourStringArray.ToList().ForEach(x => list.Add(string.Format("'{0}'", yourStringArray[i++])));

答案 6 :(得分:1)

这应该够了

string txtText = "this is a string";
string[] words = txtText.Split(' ');
txtText =  @"""" + string.Join(@"""", words) + @""""; 

输出

 "this"is"a"string"

答案 7 :(得分:0)

protected void btnSend_Click(object sender, EventArgs e)
     {
            string input = "It's going to be a fine day";
            string[] words = input.Split(' ');
            StringBuilder sb = new StringBuilder();
            foreach (string word in words)
            {
                if (!string.IsNullOrEmpty(word))
                {
                    sb.Append("\"");
                    sb.Append(word);
                    sb.Append("\" ");
                }
            }
          lblText.Text = sb.ToString();
  }