如何只将第一个“a”替换成我想要的字符窜??
10 个解决方案
#1
try...
string test = "xabada";
int index = test.IndexOf("a");
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, "*");
}
#2
or...
string test = "xabada";
test = Regex.Replace(test, @"(?<=^[^a]*)a", "*");
#3
久违了,过客兄!
#4
char[] array = s.ToCharArray();
array[s.IndexOfAny(new char[] { 'a' }, 0)] = 'N';
string str = new string(array);
MessageBox.Show(str);
array[s.IndexOfAny(new char[] { 'a' }, 0)] = 'N';
string str = new string(array);
MessageBox.Show(str);
#5
这么多星星,路过算了,使用IndexOf就ok了
#6
很好很强大!!
#7
今天休息,白天上来逛逛,终于又见一些熟悉的ID。。。
#8
static void Main(string[] args)
{
string yourStr = "**************";
string oldStr = "xabada";
if (oldStr.IndexOf('a') > -1)
{
oldStr = oldStr.Remove(oldStr.IndexOf('a'), 1).Insert(oldStr.IndexOf('a'), yourStr);
}
Console.WriteLine(oldStr);
}
#9
( ^_^ )不错嘛!学习啦!
#10
IndexOf
#1
try...
string test = "xabada";
int index = test.IndexOf("a");
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, "*");
}
#2
or...
string test = "xabada";
test = Regex.Replace(test, @"(?<=^[^a]*)a", "*");
#3
久违了,过客兄!
#4
char[] array = s.ToCharArray();
array[s.IndexOfAny(new char[] { 'a' }, 0)] = 'N';
string str = new string(array);
MessageBox.Show(str);
array[s.IndexOfAny(new char[] { 'a' }, 0)] = 'N';
string str = new string(array);
MessageBox.Show(str);
#5
这么多星星,路过算了,使用IndexOf就ok了
#6
很好很强大!!
#7
今天休息,白天上来逛逛,终于又见一些熟悉的ID。。。
#8
static void Main(string[] args)
{
string yourStr = "**************";
string oldStr = "xabada";
if (oldStr.IndexOf('a') > -1)
{
oldStr = oldStr.Remove(oldStr.IndexOf('a'), 1).Insert(oldStr.IndexOf('a'), yourStr);
}
Console.WriteLine(oldStr);
}
#9
( ^_^ )不错嘛!学习啦!
#10
IndexOf