[英]C# How to add string elements in array to string
Need assistance to do this. 需要协助才能做到这一点。 Or advice about how to add string to string elements in array? 或有关如何将字符串添加到数组中的字符串元素的建议?
We have 2 txt files with 100 or more strings in each: 我们有2个txt文件,每个文件包含100个或更多的字符串:
domain.txt DOMAIN.txt文件
domain1 DOMAIN1
domain2 DOMAIN2
.. ..
title.txt title.txt
title1 TITLE1
tilte2 tilte2
.. ..
and we have: 我们有:
string link = < ah ref="http://www.domain.com" target="_blank">title< /a> 字符串链接= <ah ref =“ http://www.domain.com” target =“ _ blank”>标题</ a>
After reading files we would have 2 string arrays - we need to replace domain.com with each string from domain.txt and title with each string from title.txt like this: 读取文件后,我们将有2个字符串数组-我们需要使用domain.txt中的每个字符串替换domain.com,并使用title.txt中的每个字符串替换title,如下所示:
< ah ref="http://www.domain1.com" target="_blank">title1< /a> <ah ref =“ http://www.domain1.com” target =“ _ blank”> title1 </ a>
< ah ref="http://www.domain2.com" target="_blank">title2< /a> <ah ref =“ http://www.domain2.com” target =“ _ blank”> title2 </ a>
.. ..
Save the result string array into 2 txt files in that way: from 1-50 strings to 以这种方式将结果字符串数组保存到2个txt文件中:从1-50个字符串到
1.txt and from 50-100 to 2.txt file 1.txt和50-100到2.txt文件
What is the best way to do this by manipulating strings with strings array elements? 通过使用字符串数组元素处理字符串的最佳方法是什么?
This is probably the simplest way to read the files: 这可能是读取文件的最简单方法:
string[] domains = File.ReadAllLines("domain.txt");
string[] titles = File.ReadAllLines("titles.txt");
To make the substitutions you can use string.Format
: 要进行替换,您可以使用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]);
}
To write the output you can use Linq: 要编写输出,可以使用Linq:
File.WriteAllLines("file1.txt", results.Take(n / 2).ToArray());
File.WriteAllLines("file2.txt", results.Skip(n / 2).ToArray());
If your template is a paramter you might want to construct the format string dynamically rather than hardcoding it. 如果模板是参数,则可能需要动态构造格式字符串,而不是对其进行硬编码。 Here is an example of how you could do that: 以下是如何执行此操作的示例:
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());
}
}
This is beautiful using LINQ and some nice methods from File
: 使用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());
It isn't clear what you want if there are more than one-hundred domain/title pairs so I split them in half. 如果有超过一百个域名/标题对,您不清楚要什么,所以我将它们分成两半。
Another way is to do it lazily like this: 另一种方法是像这样懒惰地做:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.