I’ve been trying to find a way to combine 2 strings from a single text file. The text file that I am reading has the following format:
String 1
String 2
String 3
…etc.
Sample Code:
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);
I would want to append string 2 to string 1 to have: “String1 String2”. I tried using the split method and appended them using a string builder. However, this does not work because its doing the splitting and appending per line. I also cant use “.” to combine them because they are not stored in variables. Does anyone know how to achieve this or an example? Thank you.
If you would like to append each line in your file, with a space between them, then do this:
If the lines of the file have structure, then you can define a class to hold this structure, e.g.:
This class can be used to extract the fields of each line, format them, and append these formatted strings:
You can even specify which items to append, just modify the last line e.g. like this: