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 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 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;
Related
I have a string like this:
1.1.168.192
I need to convert it to this, with the numbers intact but the order reversed:
192.168.1.1
This seems like an easy question, but I cant figure it out. I'm trying something within a for loop right now but I don't know how to make it work.
This could help:
string[] splitted = "1.1.168.192".Split('.');
Array.Reverse(splitted);
string reversed = string.Join(".", splitted);
The idea is you can split things by using a char and it creates an array, then reverse it, and then join them by using a char again it will become string again.
you could split your your string and reverse this array and join it together like this:
string reverseIP(string ip) { // ip = "1.1.168.192"
string[] ipParts = ip.split('.'); // ["1", "1", "168", "192"]
Array.Reverse(ipParts);
return String.Join(".", ipParts);
}
I am new to C#. I am trying to declare a char array that contains few string values. Trying to use the ToCharArray() method to copy the string to a char array it return character array of a string. However, I get the error saying it cannot implicitly convert char[] to char.
Here's the code I have written:
char[] country= new char[5];
country[1] = "japan".ToCharArray();
country[2] = "korea".ToCharArray();
It works when I write it like this:
char[] country= "japan".ToCharArray();
but I want to use it in an array so I can randomize and choose an element from any of 5 values assigned. I would really appreciate if anyone could help, thanks.
I believe you try to have array of char arrays:
char[][] countries = new char[5][];
countries[1] = "japan".ToCharArray();
countries[2] = "korea".ToCharArray();
Every element in the array is one character.
So country[0] is "j", country[1] is "a" and so on.
with
country[1] = "japan".ToCharArray();
you try to put an array into a char, and give you an error.
Perhaps you want a list of chars. So you can use an array of array or a list of country.
for the first country for example
List<string> country = new List<string>() {
"japan",
"korea"
};
var random = new Random();
var character = country[0][random.Next(0, country[0].Length)];
Hi I am new to programming. I would like to read a text file and take the values ( strings ) and store each character of the string in an array individually. I have used a list to take in the vales from the text file. I am finding it difficult to move them into an array and then use those values in my program. Please find me a solution if possible. Thanking you in advance.
public class file_IO
{
string[] letters = new string[] //I would like to store it in this variable
public void File_Reader()
{
string filepath = #"env.txt"; //Variable to hold string
List<string> file_lines = File.ReadAllLines(filepath).ToList();//returns array of strings into List
foreach (string line in file_lines)
{
}
}
}
Hope this will work for you!.
public char[] File_Reader()
{
string filepath = #"env.txt"; //Variable to hold string
StreamReader sr = new StreamReader(filepath);
string fileContentInString = sr.ReadToEnd();
sr.Close();
return fileContentInString.ToCharArray();
}
List<List<char>> linesAsChars = File.ReadAllLines(filepath)
.Select(l => l.ToList())
.ToList();
This will get a List of List of chars.
string implements IEnumerable<char>, so with ToList each line in the file is translated to List<char>.
Solution to "store each character of the string in an array individually" is fairly easy because string is in fact an array of char. You can do this using something like this :
char[] letters;
public void File_Reader()
{
string filepath = #"env.txt";
letters = File.ReadAllText(filePath).ToArray();
}
I'm not really sure if I have understood your question properly, but from what I have read, I will assume that you want an array of lines (which are strings).
In this case, you don't need to do much as the File.ReadAllLines() method naturally outputs an array of string variables.
Remove the for loop and replace
List<string> file_lines = File.ReadAllLines(filepath).ToList();//returns array of strings into List
with:
letters = File.ReadAllLines(filepath)
In case what you want is actually an array of every char value in your file, I would refer to #m.rogalski's answer and declare an array of char[], for example, declare:
char[] fileChars;
and then replace the line I mentioned earlier with:
fileChars = readAllText(filePath).toCharArray()
You will notice that you do not need a loop in either of the above situations. Hope I helped.
i have a string variable which receives data from web the data is in the from of string like this
string output="[1,2,3,4,5,6,7,8,9,0]";
i want to convert it into a string array [] so that i can specify each element via for each loop
string output;
into
string[] out;
PS: if any predefined method is there that will also help
You can do that using Trim And Split:
var out = output.TrimStart('[').TrimEnd(']').Split(',');
But your data looks like JSON string. So, if you are dealing with JSON instead of making your own parser try using libraries that already does that such as JSON.NET.
You can use Trim functions to remove brackets and then use Split() function to get the string array that contains the substrings in this string that are delimited by elements of a specified Unicode character.
var res = output.TrimStart('[')
.TrimEnd(']')
.Split(',');
string[] arr = output.Where(c => Char.IsDigit(c)).Select(c => c.ToString()).ToArray();
output.Substring(1, output.Length - 2).Split(',');
I know that we can use string.split() to put our data into arrays like below:
string[] strSplit = Data.Split('|');
But can we know how many array items it created? I need that number.
It will create a single array of multiple strings. Like T.S. commented, you can get the number of strings using the length property of the returned array
int length = strSplit.length
Sometime you need remove the empty entries in spilt result:
string[] strSplit = data.Split(new []{"|"}, StringSplitOptions.RemoveEmptyEntries);
And get the length like this:
int length = strSplit.Length;