2014-08-27 98 views
-4

如何更換標籤?我試試這樣:如何替換標籤?

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

替換 「\ t」 的選項卡爲同 – 2014-08-27 08:14:16

回答

-1

一標籤字符串由表示。 所以,如果你要替換由問號標籤:

url.Replace("\t","?"); 

應該做的工作。

編輯: 權,由查希爾·艾哈邁德解釋,你也需要reaffect替換函數的結果...

+1

此言爲RT,爲什麼downvote? – Ouarzy 2014-08-27 08:17:36

+0

謝謝,但我的意思是用問號替換標籤(?) – SavantKing 2014-08-27 08:18:02

+0

@SavantKing: - 我已經更新了答案! – 2014-08-27 08:18:29

1
這裏

的問題是你不重新assinging的字符串返回變量:

url = url.Replace("\n",string.Empty); 

和更換標籤嘗試\ t如其他人所說:

url = url.Replace("\t","?");