It's always valuable to differentiate the Logical core of the problem you're trying to solve from any trivial and Incidental surrounding details which are fundamentally unrelated or already solved.
As it is a given that you have saved a string to a text file, the issue being that it is not the string you wish to save, your question is actually how to Generate the desired output string from the two given input strings.
In other words, writing to a text file isn't really part of the question which actually amounts to the following.
Given two strings
"data1
data2
data3
data4
data5"
and
"dataA
dataB
dataC
dataD
dataE"
compute the string that results from combining their subsections, as dilimitted by '
'
, in a pairwise fashion. That is
"data1 - dataA
data2 - dataB
data3 - dataC
data4 - dataD
data5 - dataE".
First, We will Transform each string into a collection of strings by splitting it up on this specified delimiter.
var first = string1.Split(new[] { '
' }, StringSplitOptions.RemoveEmptyEntries);
var second = string2.Split(new[] { '
' }, StringSplitOptions.RemoveEmptyEntries);
Notice how we've used the built-in Split
method provided by .NET strings which allows us to turn any string into a collection of its substrings by specifying an array of delimiters (we only care about '
'
in this case) and whether empty string should be Excluded from the results.
Now that we have two sequences of strings, rather than two strings, we can combine their corresponding elements to produce a third sequence of strings wherein each element represents a line of the desired result.
var lines = first.Zip(second, (beginWith, endWith) => $"{beginWith} - {endWith}");
Just as we used the built-in Split
method which provides all .NET strings with the capability to be broken into sequences of substrings, we leverage the built-in Zip
method of all which provides all .NET sequences with the capability to be pairwise combined with another sequence To obtain our desired sequence of individual lines. The first argument provided to Zip
specifies the sequence to combine with and the second is a function that specifies how to Combine Pairs of corresponding elements.
Now that we have the sequence
"data1 - dataA", "data2 - dataB", "data3 - dataC", "data4 - dataD", "data5 - dataE"
In the variable lines
we just need to combine that sequence of strings into a single '
'
delimited string Which we can hand off to the FileIO.WriteTextAsync
method for output, thus completing our solution.
var result = string.Join("
", lines);
await FileIO.WriteTextAsync(file, result);
As in our previous steps, we took advantage of a built in .NET capability, the Join
method of the string
type, to combine our sequence of lines into a single string with a specified separator delimiting them.
Note that we can skip this step, improving brevity and clarity, by using the FileIO.WriteLinesAsync
to Write a sequence of strings to a file with a new line between each, which happens to be just what we want
await FileIO.WriteLinesAsync(file, lines);
Putting it all together
var first = string1.Split(new[] { '
' }, StringSplitOptions.RemoveEmptyEntries);
var second = string2.Split(new[] { '
' }, StringSplitOptions.RemoveEmptyEntries);
var lines = first.Zip(second, (beginWith, endWith) => $"{beginWith} - {endWith}");
await FileIO.WriteLinesAsync(file, lines);