我一直在尝试找到一种方法来组合单个文本文件中的 2 个字符串。我正在阅读的文本文件具有以下格式:
String 1
...ETC。示例代码:
String 2
String 3
public static IEnumerable<string> getMeaningfulLines(string filename)
{
System.IO.StreamReader file =
new System.IO.StreamReader(filename);
while (!file.EndOfStream)
{
yield return file.ReadLine();
yield return file.ReadLine();
file.ReadLine();
file.ReadLine();
file.ReadLine();
file.ReadLine();
}
}
static void Main(string[] args)
{
foreach (string line in getMeaningfulLines(@"C://Log.txt"))
{
string teststring = line;
string[] parts = line.Split(' ', ',', '-', '>', '[', ']');
StringBuilder builder = new StringBuilder();
foreach (string h in parts)
{
builder.Append(h).Append(" ");
}
string result = builder.ToString();
string cleanedString = System.Text.RegularExpressions.Regex.Replace(result, @"\s+", " ");
string trimString = cleanedString.Trim();
trimString = trimString.Remove(trimString.Length - 1);
//Console.WriteLine(trimString);
string[] parts2 = trimString.Split(' ');
string quotedstring = "'" + string.Join("','", parts2) + "'";
//Console.WriteLine(quotedstring);
string[] parts3 = quotedstring.Split(' ');
Console.WriteLine(quotedstring, quotedstring.Length);
//Console.WriteLine("1) The length of '{0}' is {1}", quotedstring, quotedstring.Length);
我想将字符串 2 附加到字符串 1 以具有:“String1 String2”。我尝试使用 split 方法并使用字符串生成器附加它们。但是,这不起作用,因为它会按行进行拆分和附加。我也不能用“。” 将它们组合起来,因为它们没有存储在变量中。有谁知道如何实现这个或一个例子?谢谢你。