代碼如下:
//Trim:刪除開頭和結尾的空白,或從開頭或結尾移除指定的字符
txbVal.Text = sValue.Trim();
//TrimEnd:从字符串的结尾处移除在字符数组中指定的字符。
txbValue.Text = sValue.TrimEnd('!');
//TrimStart:从字符串的开头移除在字符数组中指定的字符。
txbValue1.Text = sValue.TrimStart('G');
//Remove:从字符串中的指定索引位置移除指定数量的字符。
txbVal2.Text = sValue.Remove(2);
//Replace:替換字符串中指定的字符
txbVal3.Text = sValue.Replace('!', '?');
//Insert:在指定位置插入字符
txbval4.Text = sValue.Insert(0,"Your ");
txbVal.Text = sValue.Trim();
//TrimEnd:从字符串的结尾处移除在字符数组中指定的字符。
txbValue.Text = sValue.TrimEnd('!');
//TrimStart:从字符串的开头移除在字符数组中指定的字符。
txbValue1.Text = sValue.TrimStart('G');
//Remove:从字符串中的指定索引位置移除指定数量的字符。
txbVal2.Text = sValue.Remove(2);
//Replace:替換字符串中指定的字符
txbVal3.Text = sValue.Replace('!', '?');
//Insert:在指定位置插入字符
txbval4.Text = sValue.Insert(0,"Your ");
結果如下圖:
注;MSDN資料:
String.Trim 方法:將指定字元集的所有項目從這個執行個體的開頭和結尾移除。
名稱 | 說明 |
---|---|
String.Trim () | |
String.Trim (Char[]) |
String.Remove 方法 :從這個執行個體中刪除指定數目的字元。
名稱 | 說明 |
---|---|
String.Remove (Int32) | |
String.Remove (Int32, Int32) |
String.Replace 方法:以另一個指定的 Unicode 字元或 String,取代這個執行個體中指定的 Unicode 字元或 String 的所有項目。
名稱 | 說明 |
---|---|
String.Replace (Char, Char) | |
String.Replace (String, String) |