I have to write function in C# which will surround every word with double quotes. I want it to look like this:
"Its" "Suposed" "To" "Be" "Like" "This"
Run Code Online (Sandbox Code Playgroud)
Here is the code I've come up with so far, but its not working:
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();
}
Run Code Online (Sandbox Code Playgroud)
Well it depends to some degree on what you consider a 'word', but you can use a regular expression:
lblText.Text = Regex.Replace(lblText.Text, @"\w+", "\"$0\"")
Run Code Online (Sandbox Code Playgroud)
This will match any sequence of one or more 'word' characters (which in the context of regular expressions includes letters, digits, and underscores) in the string, and wrap it with double quotes.
To wrap any sequence of non-whitespace characters, you can use the \S instead of \w:
lblText.Text = Regex.Replace(lblText.Text, @"\S+", "\"$0\"")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1906 次 |
| 最近记录: |