我有一个文本文件,其中包含以下链接列表:
<A title=Accept href="https://www.google.com/url?q=https%3A%2F%2Fregister.eragenx.com%2Freferral%3Freferrer%3Demail%26invitationToken%3D1f7ae07e4cb1ed96a7fc5f6de10376d9%26email%3Dlovessoumi.tra123456789soumit90%2540gmail.com&sa=D&sntz=1&usg=AFQjCNGdIeh6LFs4H5TvysbSV7DjUk2Tuw" target=_blank>Sign Up Now For Free.</A>
使用我的程序,点击按钮时我想编辑每行文件,如:
https://register.eragenx.com/referral?referrer=email&invitationToken=fd78d9fdf6281e033b389cb14e118f85&email=lovesleeping1234567.8sleepin.g97%40gmail.com
这就是我正在尝试的,但它似乎有效,我做错了什么?
public void editline()
{
string[] lines = File.ReadAllLines("link.txt");
foreach (var line in lines)
{
line.Substring(51,171);
line.Replace("%3A", ":");
line.Replace("%2F","/");
line.Replace("%3F","?");
line.Replace("%3D" , "=");
line.Replace("%26","&");
}
File.WriteAllLines("output.txt",lines);
}
我也试过这个:
public void editline()
{
string[] lines = File.ReadAllLines("link.txt");
foreach (var line in lines)
{
HttpUtility.UrlDecode(line.Substring(51, 171));
}
File.WriteAllLines("output.txt", lines);
}
和此:
public void editline()
{
string parsedLine = null;
string[] lines = File.ReadAllLines("link.txt");
foreach (var line in lines)
{
parsedLine = line.Substring(51, 171);
parsedLine = line.Replace("%3A", ":");
parsedLine = line.Replace("%2F", "/");
parsedLine = line.Replace("%3F", "?");
parsedLine = line.Replace("%3D", "=");
parsedLine = line.Replace("%26", "&");
}
File.WriteAllLines("output.txt", lines);
}
但输出与link.txt中的链接相同,该方法在output.txt中没有变化
请帮助
答案 0 :(得分:3)
line
没有变化的原因是因为您没有存储substring
和replace
函数的结果。
应该是这样的:
string parsedLine= null;
foreach (var line in lines)
{
parsedLine = line.Substring(51,171);
parsedLine = parsedLine.Replace("%3A", ":");
parsedLine = ...
}
//use parsedLine here...
答案 1 :(得分:1)
您应该使用此处的HttpServerUtility.UrlDecode()
方法:
http://msdn.microsoft.com/en-us/library/6196h3wt(v=vs.110).aspx
答案 2 :(得分:0)
public void editline()
{
string[] lines = File.ReadAllLines("link.txt");
foreach (var line in lines)
{
line = line.Substring(51,171);
line = line.Replace("%3A", ":");
line = line.Replace("%2F","/");
line = line.Replace("%3F","?");
line = line.Replace("%3D" , "=");
line = line.Replace("%26","&");
}
File.WriteAllLines("output.txt",lines);
}
查看替换方法。它返回修改后的字符串,不会修改您传入的字符串。