方法中也提供了一种由字符数组转换为字符串的方法。
/// <summary> /// 使用指定的字符替换除首尾字符外的字符串中的字符 /// </summary> /// <param name="sourceString">原字符串</param> /// <param name="ch">指定的字符</param> /// <returns></returns> private static string ReplaceWithNewStr(string sourceString, char ch) { char[] tempChar = sourceString.ToCharArray(); if (tempChar.Length<=2) { if (tempChar.Length<=1) { tempChar[0] = ch; } else { tempChar[1] = ch; } } else { for (int i = 1; i < sourceString.Length - 1; i++) { tempChar[i] = ch; } } string str = string.Join("", tempChar); return str; }