2009-12-27 109 views
0

需要幫助來完成此操作。或者有關如何將字符串添加到數組中的字符串元素的建議?C#如何將數組中的字符串元素添加到字符串

  1. 我們有2個TXT 100個人以上的字符串中的每個文件:

    DOMAIN.txt文件
    域1
    域2
    ..
    title.txt
    TITLE1
    tilte2
    ..

  2. 我們有:

    字符串鏈接= <啊REF = 「http://www.domain.com」 目標= 「_空白」>標題</A>

  3. 讀取文件後,我們將有2字符串數組 - 我們需要用domain.txt中的每個字符串替換domain.com,並使用title.txt中的每個字符串替換如下所示的字符串:

    < ah ref =「http://www.domain1.com」target = 「_blank」> title1 </a>
    < ah ref =「http://www.domain2.com」target =「_ blank」> title2 </a>
    ..

  4. 結果字符串數組保存到2個txt文件那樣:1-50字符串
    的1.txt從50-100到2.txt文件

通過使用字符串數組元素操縱字符串來完成此操作的最佳方法是什麼?

+0

這是微不足道的,對不起,我讓別人回答。請記住,字符串是不可變的,所以你會想做arr [12] = arr [12] .replace(「x」,「y」)而不僅僅是a = arr [12]; a.replace(「x」,「y」); (假設你把東西放在一個數組中)。 MSDN是你的朋友,代碼完成也是如此。 –

+0

你說「100或更多」,但後來你想在一個文件中使用1-50,在第二個文件中使用50-100。如果有超過100行的話,第二部分將如何工作?你想在每個文件中使用一半的文件,或者每個文件中最多隻有50行文件,只需要多少個文件? –

+0

感謝您的快速回復。從1-50節省的結果我只是想知道如何選擇特殊線路保存,例如從第10個字符串到20? –

回答

2

這可能是讀取文件最簡單的方法:

string[] domains = File.ReadAllLines("domain.txt"); 
    string[] titles = File.ReadAllLines("titles.txt"); 

爲了使替換你可以使用string.Format

int n = domains.Length; 
    string[] results = new string[n]; 

    for (int i = 0; i < n; ++i) 
    { 
     results[i] = string.Format(
      @"<a href=""http://{0}"" target=""_blank"">{1}</a>", 
      domains[i], titles[i]); 
    } 

要寫出你可以使用輸出的Linq:

File.WriteAllLines("file1.txt", results.Take(n/2).ToArray()); 
    File.WriteAllLines("file2.txt", results.Skip(n/2).ToArray()); 

如果你的模板是一個參數,你可能想要構造格式str動態而不是硬編碼。這裏是你如何能做到這一點的例子:

using System; 
using System.IO; 
using System.Linq; 

class Program 
{ 
    static string escapeBraces(string s) 
    { 
     return s.Replace("{", "{{").Replace("}", "}}"); 
    } 

    static string createFormatString(string template, params string[] parameters) 
    { 
     template = escapeBraces(template); 

     for (int i =0; i < parameters.Length; ++i) { 
      template = template.Replace(
       escapeBraces(parameters[i]), 
       "{" + i + "}"); 
     } 

     return template; 
    } 

    static void Main(string[] args) 
    { 
     string template = @"<a {}href=""http://www.domain.com"" target=""_blank"">title</a>"; 
     string formatString = createFormatString(template, "www.domain.com", "title"); 

     string[] domains = File.ReadAllLines("domain.txt"); 
     string[] titles = File.ReadAllLines("title.txt"); 

     int n = domains.Length; 
     if (titles.Length != n) 
      throw new InvalidDataException("There must be the same number domains and titles."); 

     string[] results = new string[n]; 
     for (int i = 0; i < n; ++i) 
     { 
      results[i] = string.Format(formatString, domains[i], titles[i]); 
     } 

     File.WriteAllLines("file1.txt", results.Take(n/2).ToArray()); 
     File.WriteAllLines("file2.txt", results.Skip(n/2).ToArray()); 
    } 
} 
+0

這是一種形成字符串的方法,但我認爲提問者想修改現有的模板,這可能不是最好的處理方式。 –

+0

問題:ReadAllInes會自動關閉並將資源配置爲使用「使用」+ IDisposable? –

+1

@lpthnc:是的,'ReadAllLines'使用'using'塊。你可以在反射器中看到它。該實現有效地在'using'塊中打開'StreamReader',逐行讀取一行,並將它們添加到'ArrayList'中。然後使用'ArrayList.ToArray(typeof(string))'將'ArrayList'轉換爲'string []'。 – jason

2

這是美麗的使用LINQ和一些不錯的方法從File

string[] domains = File.ReadAllLines(@"C:/domains.txt"); 
string[] titles = File.ReadAllLines(@"C:/titles.txt"); 
if(domains.Length != titles.Length) { throw new InvalidOperationException(); } 
string link = "<a href=\"http://www.{0}.com\" target=\"_blank\">{1}</a>"; 
var results = domains.Select((domain, i) => String.Format(link, domain, titles[i])); 
File.WriteAllLines("results1.txt", results.Take(results.Length/2).ToArray()); 
File.WriteAllLines("results2.txt", results.Skip(results.Length/2).ToArray()); 

目前尚不清楚你想要什麼,如果有超過一百個域名/標題對,所以我把它們分成了兩半。

+0

哇,幾乎和Python一樣酷; –

+0

當我們有Zip時,它在.NET 4.0中更加優雅。 –

+0

@Mark Byers:你是對的先生。 – jason

0

另一種方法是做懶洋洋地是這樣的:

 static IEnumerable<string> ReadLinesLazily(string file) 
    { 
     using (var reader = new StreamReader(file)) 
     { 
      string line = null; 
      while ((line = reader.ReadLine()) != null) 
      { 
       yield return line; 
      } 
     } 
    } 

    static void Combine() 
    { 
     const string link = "<a href=\"{0}\">{1}</a>"; 
     var links = ReadLinesLazily("domains.txt").Zip(ReadLinesLazily("titels.txt"), (d, t) => String.Format(link, d, t)) 
     // write links here 
    } 
+1

可能值得一提的是,Zip首先包含在.NET 4.0中。 –

相關問題