字符串中替换某一字符

yksyuan 2011-04-07 12:06:00

string test = "ababa";
int index = test.IndexOf("a");
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, "*");
}


这个输出的结果是 *baba。
想把第二个a替换成*。 输出成:ab*ba
当然 test 是动态的。。位数不定。
...全文
152 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yksyuan 2011-04-07
  • 打赏
  • 举报
回复
结贴早了。。。12楼 15楼的是正确的。
11楼的子夜 代码有问题。
刚才没仔细看。
if (i == count / 2)
这样肯定错了。。。。
做个标记吧。以后查阅时方便
ming_mu 2011-04-07
  • 打赏
  • 举报
回复

string test = "ababa";
int index = test.IndexOf("a");
if (index > -1)
{

int a= test .IndexOf("a", test .IndexOf("a") + 1);
test = test .Remove(a,1).Insert(a, "*");
}

yksyuan 2011-04-07
  • 打赏
  • 举报
回复
谢谢大家。结贴了
呵呵 正确的是 11楼 和12楼的两位 朋友
分不多。。。
谢谢各位了
yksyuan 2011-04-07
  • 打赏
  • 举报
回复
7楼的 这次虽然还不对。但也差不多。
9楼的兄弟 完全不对啊。 和我贴出的代码 没什么不同么。就是 代码更多而已。。悲剧啊
10楼的兄弟 很强悍。呵呵 完全正确。。。
q107770540 2011-04-07
  • 打赏
  • 举报
回复
void Main()
{
string test = "ababa";
test=Regex.Replace(test,"(a[^a]*)a","$1*"); //这样写更严谨一些
Console.WriteLine(test);//ab*ba
}
子夜__ 2011-04-07
  • 打赏
  • 举报
回复
 string test = "ababa";
int count = test.ToCharArray().Length;
for (int i = 0; i < test.ToCharArray().Length; i++)
{
if (i == count / 2)
{
Response.Write(test.ToCharArray()[i].ToString().Replace("a", "*").ToString());
}
else
{
Response.Write(test.ToCharArray()[i].ToString());
}
}


ab*ba
q107770540 2011-04-07
  • 打赏
  • 举报
回复

void Main()
{
string test = "ababa";
test=Regex.Replace(test,"(a[^a])a","$1*");
Console.WriteLine(test);//ab*ba
}
anbam 2011-04-07
  • 打赏
  • 举报
回复
string test = "ababa";
int index = test.IndexOf("a");
if (index > -1)
{
index = test.IndexOf("a",index);
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, "*");
}
}

快溜 2011-04-07
  • 打赏
  • 举报
回复
替换两次
快溜 2011-04-07
  • 打赏
  • 举报
回复
string test = "ababa";
int index = test.IndexOf("a");
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, " ");
}
index = test.IndexOf("a");
if (index > -1)
{
test = test.Remove(index, 1).Insert(index, "*");
}
test.Replace(" ", "a");
yksyuan 2011-04-07
  • 打赏
  • 举报
回复
1楼的:ssp2009兄
test.split("a")[i].replace("a","*")
这句话 取到的值根本就没有a 。后边根本就不存在替换了
split不是按a切割的么。取到的是 b。。。

[Quote=引用 1 楼 ssp2009 的回复:]
for (int i=0 i<test.split("a").length,i++){
if(i=1)
test.split("a")[i].replace("a","*")
test+=test.split("a")[i].tostring()
}
[/Quote]
yksyuan 2011-04-07
  • 打赏
  • 举报
回复
这个应该很容易吧。
我上面代码贴的 代码 是替换 第一次出现的 字符"a”
现在 只是想 知道 如何 替换 第二次出现的 字符"a"
yksyuan 2011-04-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ranen2010 的回复:]
string test = "ababa";
Response.Write(test.Replace("a", "*"));
[/Quote]
兄弟 你没看题目啊。
我要 转换成 ab*ba
你这到简单。直接把所有的a 都替换成*了
小童 2011-04-07
  • 打赏
  • 举报
回复

string test = "ababa";
Response.Write(test.Replace("a", "*"));
快溜 2011-04-07
  • 打赏
  • 举报
回复
for (int i=0 i<test.split("a").length,i++){
if(i=1)
test.split("a")[i].replace("a","*")
test+=test.split("a")[i].tostring()
}
虾米贝贝 2011-04-07
  • 打赏
  • 举报
回复
Replace(a,"*")

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧