2009-12-27 110 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中。 –

相关问题