user1767833
user1767833

Reputation: 129

How to add a character to beginning and ending of a String in C#?

I have a string, lets say it's myStr = "My name is user something something";

I want to change this string to:

'My name is user something something'

I want to convert the string from:

My name is user something something

to:

'My name is user something something'

I tried several methods but none worked, looks like easy but I'm missing something. Can you please make a code block which adds "'" character to beginning and ending of a string. Thank you.

Edit: My code;

         StringBuilder sb = new StringBuilder();

         gecici = gecici + "'";//I think this adds to the ending
         sb.Append(gecici);
         sb.Insert(0, "'");// this to the beginning
         gecici = sb.ToString();

Upvotes: 1

Views: 6646

Answers (3)

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You can use string.Format method:

myStr = "My name is user something something";
var newStr = string.Format("'{0}'", myStr);

Upvotes: 9

Grant Thomas
Grant Thomas

Reputation: 45083

You want the value to be wrapped in single quotes as part of the value? Just add them like so:

string myStr = "'My name is user something something'";

If you're actually getting the string in the form of a variable and want to alter that, then do:

myStr = "'" + myStr + "'";

Upvotes: 1

Fredou
Fredou

Reputation: 20100

one way would be

   myStr = "'" + "My name is user something something" + "'";

Upvotes: 3

Related Questions