2014-08-27 1 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 ("\ п", "?"); не делает работу. Итак, как это сделать? У моего сообщения много кода. Но у меня нет текста к типу

Спасибо


это полный код:

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

A строка табуляции представлена ​​\t. Так что если вы хотите вкладку знаком вопроса замены:

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

должен делать эту работу.

Edit: Право, как и объяснить Захир Ахмед, вы должны также reaffect результат функции Replace ...

+1

То же замечание, как RT, почему downvote? – Ouarzy

+0

Спасибо, но я имею в виду заменить вкладки вопросительным знаком (?) – SavantKing

+0

@SavantKing: - Я обновил ответ! –

1

Проблема здесь вы повторно не assinging строку обратно в переменную:

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

и заменить вкладки попытаться \ т, как уже отмечалось другими:

url = url.Replace("\t","?"); 
Смежные вопросы