| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 764 人关注过本帖
标题:一个查找并且替换的算法
只看楼主 加入收藏
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
结帖率:0
收藏
 问题点数:0 回复次数:1 
一个查找并且替换的算法
namespace ConsoleApp
{

class Program
{

public static void Main()
{
string input=Console.ReadLine();
string output = Program.Replace (input, "ab", "00");
System.Console.ReadLine();

}

private static string Replace(string source, string a, string b)
{
string result = source;
int position = 0;//发现的位置
int len = 0;//
int num = 0;//记录发现的要替换的个数

for (int i = 0; i < result.Length - 1; i++)
{
if (result[i] == a[0])
{
position = i;
len = 0;
//int j = 0;

while (len<=a.Length-1 && result[position + len] == a[len])
{
len++;
}

if (len == a.Length)
{
Console.WriteLine("find a positon");
num++;
i = position+len;
Console.WriteLine("the posion is " + position + "\n the num is " + num);
continue;
}

if (a.Length == b.Length)
{

for (int j = position; j < a.Length - 1; j++)
{
result[j] = a[j];
}
}
else if (a.Length > b.Length)
{

}
else
{

}
}
}
return "";
}
}
}

暂时实现了查找符合条件的个数和位置,替换的时候我想这样
if (a.Length == b.Length)
{

for (int j = position; j < a.Length - 1; j++)
{
result[j] = a[j];
}
}
else if (a.Length > b.Length)
{

}
else
{

}
要考虑三种情况,要查找的和要替换的字符串的长度相等,大于,小于。
相等就可以直接替换相应位置的字符就可以了,可是发现错误
////..............................
无法对属性或索引器“string.this[int]”赋值 -- 它是只读的
//.....................
我的思路是搞一个数组存放没有替换的和替换了的所有字符,不知各位有没有更好的呢,在这里先谢谢你的回复!!
搜索更多相关主题的帖子: 算法 div target highlighting 
2007-09-06 10:13
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
收藏
得分:0 
学习一下...

msdn == 葵花宝典!!!
QQ:122768959
2007-09-06 11:39
快速回复:一个查找并且替换的算法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011353 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved