我怎樣才能做到格式字符串自定義格式:轉換[字符串]的ToString([自定義格式])
int value = 5000;
String.Format("{0:## ###}", value);
value.ToString("##");
但值作爲字符串,而無需使用轉換爲多少? 是這樣的:
String.Format("{0:## ###}, "5000");
**更新:
我試圖創建一個通用的功能:
public string FormatString(string value, string format = "") {
if (value == null){
return "";
}
return String.Format("{0:" + format + "}", value);
}
public bool OtherFunction(id){
var data = dc.GetData(id);
ViewBag.DescriptionText = FormatString(data.Description).Replace("\n", "<br />");
ViewBag.Phone = FormatString(data.Phone, "(##) ####-#####");
ViewBag.City= FormatString(data.City);
[...]
}
恐怕你真的不清楚你想達到什麼目的。 「## ###」格式專門用於格式化數字。當給予「你好世界」時,你會期待它做什麼? –
你不能,如果你需要格式化一個字符串,你必須爲所述字符串編寫一個格式化程序,這可能比簡單地將字符串轉換爲數字要複雜得多。 – Tobberoth
使用「Hello World」,我將調用函數而不指定格式。所以它將只是「Hello World」.. –