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;
Related
I am getting back an array of emails. It is an array but it is just one long string of emails separated by commas.
string[] emails - is returning an array of emails but it is just one string.
emails = {"fjdksalg#gmail.com, jkgior#gmail.com, fjkgftroe#gmail.com,"}
How can i get these emails separated into individual strings?
You can use string.Split() to separate a string by a specific character.
string longString = "fjdksalg#gmail.com,jkgior#gmail.com,fjkgftroe#gmail.com";
string[] emailArray = longString.Split(',');
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;
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.
It keeps giving me an error message here and I don't understand why.
int[] user31 = new int[53];
user31 = System.IO.File.ReadLines("ratings.txt").Skip(1675).Take(53).ToArray();
Because ReadLines returns strings, you know. It is right there in the documentation:
http://msdn.microsoft.com/en-us/library/dd383503(v=vs.110).aspx
and you just do "TOArray".
If you would parse the rows before doing ToArray....
Skip(1675).Take53.Select (x=> Int.Parse(x)).ToArray()
(or along this line)
You would get an array of ints, but calling ToArray on an enum of string returns an array of strings.
Just look at the return type of System.IO.File.ReadLines methods. It is String[].
You call Skip and Take as a result you will have an IEnumerable<String> calling ToArray again will give you a String[] not int[].
Because each line of a text file is just that, text. If you can guarantee that each line is actually an integer, you can do something like this:
int[] user31 = new int[53];
string[] lines = System.IO.File.ReadLines("ratings.txt").Skip(1675).Take(53).ToArray();
int i = 0;
foreach (var line in lines)
{
user31[i++] = Convert.ToInt32(lines[i]);
}
ReadLines returns strings with an array
I assume that your ratings.txt file contain int values in each line
you need to convert string array to int array ..
int[] user31 = new int[53];
user31= System.IO.File.ReadLines("ratings.txt").Skip(1675).Take(53).ToArray().Select(n => Convert.ToInt32(n)).ToArray();
This is because the user31 is an integer array but ReadLine returns string[].
Try like this:
List<string> str =new List<string>(new string[] {"123", "234","345","456","678","678","890"});
List<int> a = str.Skip(1675).Take(53).ConvertAll(new Converter<string, int>(int.Parse));
No need to explicitly parse it yourself and return an array. ConvertAllwill do the job to return an array.
EDIT:
Included a running test case using ConvertAll function.
I'm taking a URL that looks like this:
some_site.com/full/path/page.aspx?label[0]=a_value&label[1]=b_value&label[2]=c_value
The indexed number is generated, so there's a dynamic number of these 'label[x]' values every time.
What would the simplest way of parsing these all into a String[] named 'Label' in ASP/C#.NET 4.0?
You should use a NameValueCollection instead of array of Strings.
NameValueCollection queryParameters = new NameValueCollection();
string[] querySegments = queryString.Split('&');
foreach(string segment in querySegments)
{
string[] parts = segment.Split('=');
if (parts.Length > 0)
{
string key = parts[0].Trim(new char[] { '?', ' ' });
string val = parts[1].Trim();
queryParameters.Add(key, val);
}
}
To get the number of the label withing the square brackets, use Regular Expressions.
regxObj = new Regex(#"\[(.*?)\]");
Have you thought about enumerating the entries in the Request.Querystring collection?
You can start by taking the substring from the index of '?' to the end, then split by '&'.
Then you can either loop through that list and split by '=' and take the second element, or the substring of each of those starting after the index of '='.
If you do want it as just and array of strings for some reason, this one line will probably work.
String[] labels = (from substring in s.Substring(s.IndexOf('?') + 1).Split('&') select substring.Substring(substring.IndexOf('=') + 1)).ToArray();
edit: Do note that this disregards what the actual labels are, as well as their numbers; if there's something other than named, numbered label[n] tags, those will be added as to the array as well.
make a parameter called length
some_site.com/full/path/page.aspx?length=4&label[0]=a_value&label[1]=b_value&label[2]=c_value...
then that will be easy to parse on the other side already knowing the length
if you know the length , then you know how many times to iterate through a loop to read the values of querystring
-or-
don't have a variable amount of parametes , use one , and use any special character to seperate them, then split the value by the seperating char on the other side