简体   繁体   English

将字符串数组转换为字符数组

[英]Converting string array to char array

I want to read.txt file, extract every distinct from it and save it to array.我想读取.txt 文件,从中提取每个不同的内容并将其保存到数组中。 So far I came up with this:到目前为止,我想出了这个:

string text = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie\TextSample.txt");
string uniqueLetters = new string(text.Distinct().ToArray()); 

I couldn't find any way to save those distinct letters to a char array.我找不到任何方法将这些不同的字母保存到 char 数组中。 Now I want to convert the uniqueLetters array to a char array.现在我想将uniqueLetters数组转换为char数组。 I've been trying through certain things like creating a new char[] array and assigning uniqueLetter value in a for loop.我一直在尝试某些事情,例如创建一个新的char[]数组并在for循环中分配uniqueLetter值。 ToCharArray() also failed me. ToCharArray()也让我失望了。 Does anybody have any ideas how to do it?有人知道怎么做吗?

The return value type is char array and not string.返回值类型是字符数组而不是字符串。

string text = "AABBCC";
var uniqueLetters = text.Distinct().ToArray();

Output (array of chars): Output(字符数组):

A, B, C. A,B,C。

Edit: Dont forget:编辑:不要忘记:

using System.Linq;

The ToArray method returns a char[] , that is, a char array. ToArray方法返回一个char[] ,即一个char数组。 Use it like this in your code:在您的代码中像这样使用它:

string text = File.ReadAllText(@"C:\Users\ASUS\Desktop\szyfrowanie\TextSample.txt");
        char[] uniqueLetters = text.Distinct().ToArray();

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

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