String two = "dell.lnk"; String[] arr2 = new string[3]; //注:结果为de, trimend会删除结尾中包含.lnk这四个字母的字符,没有顺序 arr2[0] = two.TrimEnd(".lnk".ToCharArray()); arr2[1] = two.Replace(".lnk", ""); Console.WriteLine("TrimEnd Test:"); Console.WriteLine("Old : {0}", two); for (int i = 0; i < arr2.Length; i++) { Console.WriteLine("Way {0}: {1}", (i + 1), arr2[i]); } String oldstr = "a,b,c,d,"; String[] arr = new string[5]; arr[0]= oldstr.Trim(','); arr[1] = oldstr.TrimEnd(','); arr[2] = oldstr.Remove(oldstr.Length - 1); arr[3] = oldstr.EndsWith(",") ? oldstr.Substring(0, oldstr.Length - 1) : oldstr; Console.WriteLine("--------------------------"); Console.WriteLine("Old : {0}",oldstr); for (int i = 0; i < arr.Length;i++ ) { Console.WriteLine("Way {0}: {1}",(i+1),arr[i]); } Console.ReadLine();
总结:
1.去除末尾单个字符, 最好是TrimEnd;
2.去除末尾多个字符, 那一定要用substring, replace(如果不能确保只匹配末尾的话, 也不可靠)