Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding chars before and after each word in a string

Tags:

c#

Imagine we have a string as :

String mystring = "A,B,C,D";

I would like to add an apostrophe before and after each word in my string.Such as:

"'A','B','C','D'"

How can i achieve that?

like image 451
Sin5k4 Avatar asked Jun 19 '13 12:06

Sin5k4


2 Answers

What's your definition of a word? Anything between commas?

First get the words:

var words = mystring.Split(',');

Then add the apostrophes:

words = words.Select(w => String.Format("'{0}'", w));

And turn them back into one string:

var mynewstring = String.Join(",", words);
like image 92
kevingessner Avatar answered Oct 07 '22 17:10

kevingessner


mystring = "'" + mystring.replace(",", "','") + "'";
like image 26
Rob Avatar answered Oct 07 '22 17:10

Rob