简体   繁体   English

使用 UWP C# 中的两个不同字符串创建一个文本文件

[英]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.我想使用两个不同的字符串在 c# UWP 中创建一个 .txt 文件。

string1 contains: data1 \n data2 \n data3 \n data4 \n data5 string1 包含:data1 \n data2 \n data3 \n data4 \n data5

string2 contains: dataA \n dataB \n dataC \n dataD \n dataE string2 包含:dataA \n dataB \n dataC \n dataD \n dataE

I want the text file to look like NewTextUWP我希望文本文件看起来像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我得到的 Output 是NewTextUWP

any sort of help is appreciated, Thank you.任何形式的帮助表示赞赏,谢谢。

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.由于您已将字符串保存到文本文件,问题在于它不是您要保存的字符串,您的问题实际上是如何从两个给定的输入字符串生成所需的 output 字符串。

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 \n data2 \n data3 \n data4 \n data5" 

and

"dataA \n dataB \n dataC \n dataD \n dataE" 

compute the string that results from combining their subsections, as dilimitted by '\n' , in a pairwise fashion.以成对的方式计算由'\n'分隔的子部分组合得到的字符串。 That is那是

 "data1 - dataA \n data2 - dataB \n data3 - dataC \n data4 - dataD \n 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[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

var second = string2.Split(new[] { '\n' }, 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 '\n' in this case) and whether empty string should be Excluded from the results.请注意我们如何使用 .NET 字符串提供的内置Split方法,该方法允许我们通过指定分隔符数组将任何字符串转换为其子字符串的集合(在这种情况下,我们只关心'\n' )以及是否空字符串应从结果中排除。

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.就像我们使用内置的Split方法一样,它为所有 .NET 字符串提供了被分解为子字符串序列的能力,我们利用内置的Zip方法,它提供了所有 .NET 序列成对组合的能力序列 获得我们想要的单行序列。 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.提供给Zip的第一个参数指定要组合的序列,第二个参数是 function,指定如何组合成对的相应元素。

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 '\n' delimited string Which we can hand off to the FileIO.WriteTextAsync method for output, thus completing our solution.在变量lines中,我们只需将该字符串序列组合成一个以'\n'分隔的字符串,我们可以将其交给 output 的FileIO.WriteTextAsync方法,从而完成我们的解决方案。

var result = string.Join("\n", 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.与前面的步骤一样,我们利用内置的 .NET 功能( string类型的Join方法)将我们的行序列组合成一个字符串,并使用指定的分隔符将它们分隔开。

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请注意,我们可以跳过这一步,提高简洁性和清晰度,通过使用FileIO.WriteLinesAsync将字符串序列写入文件,每个文件之间有一个新行,这恰好是我们想要的

await FileIO.WriteLinesAsync(file, lines);

Putting it all together把它们放在一起

var first = string1.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

var second = string2.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

var lines = first.Zip(second, (beginWith, endWith) => $"{beginWith} - {endWith}");

await FileIO.WriteLinesAsync(file, lines);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM