日期:2014-05-16 浏览次数:21468 次
string strA = "1absjjkcbfka2rsbcfak2bfka1";
我想留住前面一位与最后一位,其它的替换成*字符,要怎么做呢?
string source = "1absjjkcbfka2rsbcfak2bfka1";
string patten = @"(?<=\S)\S(?=\S)";
Regex reg = new Regex(patten);
source = reg.Replace(source, "*"); //source 就是结果
strA[0] + "".PadLeft(strA.Length - 2, '*') + strA[strA.Length - 1]
string s1 = "1fdljld fh fdfhdnfld f1";
string s2 = s1.Substring(1, s1.Length - 1-1);
char[] chars = s2.ToCharArray(); //因为字符串是不可变的,所以这里将字符串拆分为字符到数组chars
for (int i = 0; i < chars.Length; i++)
{
chars[i] = '*'; //在这里将数组的每一项都替换为*
}
s2 = new string(chars); //将chars字符数组组成字符串
string str = s1.Substring(0, 1) + s2 + s1.Substring(s1.Length - 1); //最后何必整个字符串。
Console.WriteLine(str); //输出结果是: 1**************1
Console.ReadKey();
或者另外一种方法:
string s1 = "1fdljld fh fdfhdnfld f1";
char[] ch1 = s1.ToCharArray();
for (int i = 0; i < ch1.Length; i++)
{
if (i == ch1.Length - 1-1)
&nb