![](/img/trans.png)
[英]How to add a symbol character in the middle of string but not in the beginning or in the end of the string in C#
[英]How to add a character to beginning and ending of a String in C#?
我有一個string
,可以說是myStr = "My name is user something something";
我想將此字符串更改為:
'My name is user something something'
我想從轉換字符串:
我的名字是用戶某事
至:
“我的名字是用戶,有些東西”
我嘗試了幾種方法,但是都沒有用,看起來很簡單,但是我缺少了一些東西。 您能否制作一個在字符串的開頭和結尾添加"'"
字符的代碼塊。 謝謝。
編輯:我的代碼;
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();
您可以使用string.Format
方法:
myStr = "My name is user something something";
var newStr = string.Format("'{0}'", myStr);
一種方法是
myStr = "'" + "My name is user something something" + "'";
您想將值作為值的一部分用單引號引起來嗎? 像這樣添加它們:
string myStr = "'My name is user something something'";
如果您實際上是以可變形式獲取字符串,並且想要更改該字符串,請執行以下操作:
myStr = "'" + myStr + "'";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.