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"
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();
}
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\"")
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\"")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With