Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

create a text file using two different strings in UWP C#

I want

I want to create a .txt file in c# UWP using two different strings.

string1 contains: data1 data2 data3 data4 data5

string2 contains: dataA dataB dataC dataD dataE

I want the text file to look like NewTextUWP

I've tried

private async void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
    {
        FolderPicker openFileDialog = new FolderPicker
        {
            SuggestedStartLocation = PickerLocationId.Desktop,
            ViewMode = PickerViewMode.List
        };
        openFileDialog.FileTypeFilter.Add(".txt");
        StorageFolder destinationfolder = await openFileDialog.PickSingleFolderAsync();
        StorageFile file = await destinationfolder.CreateFileAsync("newtextUWP.txt", CreationCollisionOption.ReplaceExisting);
        await FileIO.WriteTextAsync(file, string1 + string2);
    }

Output i am getting is NewTextUWP

any sort of help is appreciated, Thank you.

question from:https://stackoverflow.com/questions/65849438/create-a-text-file-using-two-different-strings-in-uwp-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...