简体   繁体   中英

Converting string array to char array

I want to read.txt file, extract every distinct from it and save it to array. 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. Now I want to convert the uniqueLetters array to a char array. I've been trying through certain things like creating a new char[] array and assigning uniqueLetter value in a for loop. ToCharArray() also failed me. 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):

A, B, C.

Edit: Dont forget:

using System.Linq;

The ToArray method returns a char[] , that is, a char array. Use it like this in your code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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