如何更换标签?我这样试试:
foreach (var url in urlList)
{
try
{
stopwatch.Start();
requester.DownloadString(url);
url.Replace("\n",string.Empty);
if (url.Contains("/select/"))
{
url.Replace(string.Empty, "?");
}
}
catch (Exception e)
{
Console.WriteLine("An error occured while attempting to connect to {0}", url);
}
finally
{
stopwatch.Stop();
//We use the counter for a friendlier url as the current ones are unwieldly
times.Add("Url " + counter, stopwatch.Elapsed);
counter++;
stopwatch.Reset();
}
}
但是这个:url.Replace(“\ n”,“?”);不做这个工作。那么如何管理呢?我的帖子有很多代码。但我没有要输入的文字
谢谢
这是完整的代码:
public class Program
{
static void Main(string[] args)
{
//Consider making this configurable
const string sourceFile = "testSolar.txt";
var requester = new WebClient();
var times = new Dictionary<string, TimeSpan>();
var stopwatch = new System.Diagnostics.Stopwatch();
//Add header so if headers are tracked, it will show it is your application rather than something ambiguous
requester.Headers.Add(HttpRequestHeader.UserAgent, "Response-Tester-Client");
var urlList = new List<string>();
//Loop through the lines in the file to get the urls
try
{
stopwatch.Start();
using (var reader = new StreamReader(sourceFile))
{
while (!reader.EndOfStream)
{
urlList.Add(reader.ReadLine());
urlList.Remove("\n");
}
}
}
catch (Exception e)
{
Console.WriteLine("An error occured while attempting to access the source file at {0}", sourceFile);
}
finally
{
//Stop, record and reset the stopwatch
stopwatch.Stop();
times.Add("FileReadTime", stopwatch.Elapsed);
stopwatch.Reset();
}
//Try to connect to each url
var counter = 1;
foreach (var url in urlList)
{
try
{
stopwatch.Start();
requester.DownloadString(url);
url.Replace("\t","?");
if (url.Contains("/select/"))
{
url.Replace(string.Empty, "?");
}
}
catch (Exception e)
{
Console.WriteLine("An error occured while attempting to connect to {0}", url);
}
finally
{
stopwatch.Stop();
//We use the counter for a friendlier url as the current ones are unwieldly
times.Add("Url " + counter, stopwatch.Elapsed);
counter++;
stopwatch.Reset();
}
}
//Release the resources for the WebClient
requester.Dispose();
//Write the response times
foreach (var key in times.Keys)
{
Console.WriteLine("{0}: {1}", key, times[key].TotalSeconds);
}
Console.ReadKey();
}
}
这是在做这项工作:
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
line = line.Replace("\t", "?");
urlList.Add(line);
}
答案 0 :(得分:1)
这里的问题是你没有将字符串重新分配给变量:
url = url.Replace("\n",string.Empty);
并替换标签try \ t,如其他人所述:
url = url.Replace("\t","?");
答案 1 :(得分:-1)
标签字符串由\t
表示。
因此,如果您想用问号替换制表符:
url.Replace("\t","?");
应该做的工作。
编辑: 是的,正如Zaheer Ahmed解释的那样,你还需要重新影响Replace函数的结果......