I have an external text file which has numbers in it. like 4 54 12 32 separated by spaces.
I want to be able to read all the numbers and add them to a list.
static void Main(string[] args)
{
List<int> numbers;
numbers = new List<int>();
StreamReader file = new StreamReader("C:\\text.txt");
while (!file.EndOfStream)
{
string line = file.ReadLine();
Console.Write(line + " ");
}
}
ReadLine reads the whole line so I cannot separate the individual numbers and convert them to ints and I have tried Read which reads the character code of each number rather than the number itself.
Try Splitting the line by spaces
string [] numbers = file.ReadLine().Split(new char[]{' '},
StringSplitOptions.RemoveEmptyEntries);
Split method of string object ( http://msdn.microsoft.com/fr-fr/library/System.String.Split%28v=vs.110%29.aspx ) is what you're looking for.
This method should help you.
public static IEnumerable<int> ReadInts(string path)
{
var txt = File.ReadAllText(path);
return Regex.Split(txt, #"\s+").Select(x => int.Parse(x));
}
You can use File.ReadAllText method:
var numbers = File.ReadAllText("C:\\text.txt")
.Split()
.Where(x => x.All(char.IsDigit))
.Select(int.Parse)
.ToList();
Related
Consider a number of strings, which are assumed to contain "keys" of the form "Wxxx", where x are digits from 0-9. Each one can contain either one only, or multiple ones, separated by ',' followed by two spaces. For example:
W123
W432
W546, W234, W167
The ones that contain multiple "keys" need to be split up, into an array. So, the last one in the above examples should be split into an array like this: {"W546", "W234", "W167"}.
As a quick solution, String.Split comes to mind, but as far as I am aware, it can take one character, like ','. The problem is that it would return an array with like this: {"W546", " W234", " W167"}. The two spaces in all the array entries from the second one onwards can probably be removed using Substring, but is there a better solution?
For context, these values are being held in a spreadsheet, and are assumed to have undergone data validation to ensure the "keys" are separated by a comma followed by two spaces.
while ((ws.Cells[row,1].Value!=null) && (ws.Cells[row,1].Value.ToString().Equals("")))
{
// there can be one key, or multiple keys separated by ','
if (ws.Cells[row,keysCol].Value.ToString().Contains(','))
{
// there are multiple
// need to split the ones in this cell separated by a comma
}
else
{
// there is one
}
row++;
}
You can just specify ',' and ' ' as separators and RemoveEmptyEntries.
Using your sample of single keys and a string containing multiple keys you can just handle them all the same and get your list of individual keys:
List<string> cells = new List<string>() { "W123", "W432", "W546, W234, W167" };
List<string> keys = new List<string>();
foreach (string cell in cells)
{
keys.AddRange(cell.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}
Split can handle strings where's nothing to split and AddRange will accept your single keys as well as the multi-key split results.
You could use an old favorite--Regular Expressions.
Here are two flavors 'Loop' or 'LINQ'.
static void Main(string[] args)
{
var list = new List<string>{"W848","W998, W748","W953, W9484, W7373","W888"};
Console.WriteLine("LINQ");
list.ForEach(l => TestSplitRegexLinq(l));
Console.WriteLine();
Console.WriteLine("Loop");
list.ForEach(l => TestSplitRegexLoop(l));
}
private static void TestSplitRegexLinq(string s)
{
string pattern = #"[W][0-9]*";
var reg = new Regex(pattern);
reg.Matches(s).ToList().ForEach(m => Console.WriteLine(m.Value));
}
private static void TestSplitRegexLoop(string s)
{
string pattern = #"[W][0-9]*";
var reg = new Regex(pattern);
foreach (Match m in reg.Matches(s))
{
Console.WriteLine(m.Value);
}
}
Just replace the Console.Write with anything you want: eg. myList.Add(m.Value).
You will need to add the NameSpace: using System.Text.RegularExpressions;
Eliminate the extra space first (using Replace()), then use split.
var input = "W546, W234, W167";
var normalized = input.Replace(", ",",");
var array = normalized.Split(',');
This way, you treat a comma followed by a space exactly the same as you'd treat a comma. If there might be two spaces you can also replace that:
var input = "W546, W234, W167";
var normalized = input.Replace(" "," ").Replace(", ",",");
var array = normalized.Split(',');
After trying this in .NET fiddle, I think I may have a solution:
// if there are multiple
string keys = ws.Cells[row,keysCol].Value.ToString();
// remove spaces
string keys_normalised = keys.Replace(" ", string.Empty);
Console.WriteLine("Checking that spaces have been removed: " + keys3_normalised + "\n");
string[] splits = keys3_normalised.Split(',');
for (int i = 0; i < splits.Length; i++)
{
Console.WriteLine(splits[i]);
}
This produces the following output in the console:
Checking that spaces have been removed: W456,W234,W167
W456
W234
W167
I have this string that is delimited with char 1 character:
3/1k
this js script may help to see the string
var s="3/1k";
alert(s);
I tried with
string[] s1 = Str.Split(new Char[] { (char)1 },StringSplitOptions.None);
string[] s2 = Str.Split((char)1);
string[] s3 = Str.Split('');//copy and paste of (char)1
string[] s4 = System.Text.RegularExpressions.Regex.Split( Str((char)1).ToString());
I'd like to split it with delimiter (char 1)
So I should get an array with
s[0]="3/1";
s[1]="";
s[2]="";
s[3]="k";
How can I do it with C#?
I think you have to use '1' instead of (char)1
public static void Main(string[] args){
string str = "3/1k";
string[] v = str.Split('1');
foreach(string i in v)
Console.WriteLine(i);
}
Hope this helps!
Both of the first two suggestions you suggest work fine for me if I start with:
var Str = "3/1\u0001\u0001\u0001k";
It would seem that perhaps U+0001 is not actually present in the compiled string. If you are putting control characters into source (rather than e.g. reading them from a file) then it is better to escape them as per C# character escapes, rather than depend upon unprintables in the C# file.
The Split method returns an error.
static void Main(string[] args)
{
string[] ebook = File.ReadLines("C:\\Users\\Michael\\Downloads\\Documents\\x.txt").ToArray();
string[] words = ebook.Split(' ');
}
Not File.ReadLines(...).Split(' ') but ReadAllText(...).Split()
string[] words = File.ReadAllText(path).Split();
You can use the Split() to split by every white-space including new-line characters or tabs.
You should use File.ReadAllText:
var ebook = File.ReadAllText("C:\\Users\\Michael\\Downloads\\Documents\\x.txt");
var words = ebook.Split(' ');
You were using File.ReadLines that returns an IEnumerable<string> representing each line of your .txt, but if you want to split all words it is more "comfortable" to split from a single string, and File.ReadAllText returns a string with all the text in your file.
File.ReadAllText documentation
I want to split a string into 2 arrays, one with the text that's delimited by vbTab (I think it's \t in c#) and another string with the test thats delimited by vbtab (I think it's \n in c#).
By searching I found this (StackOverFlow Question: 1254577):
string input = "abc][rfd][5][,][.";
string[] parts1 = input.Split(new string[] { "][" }, StringSplitOptions.None);
string[] parts2 = Regex.Split(input, #"\]\[");
but my string would be something like this:
aaa\tbbb\tccc\tddd\teee\nAccount\tType\tCurrency\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00
so in the above code input becomes:
string input = "aa\tbbb\tccc\tddd\teee\nAccount\tType\tPersonal Current Account\tCurrency\tGBP\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00\n30OCT13\tLoan Repayment\t1,234.56-\n\tType\t30-Day Notice Savings Account\tCurrency\tGBP\tBalance\t983,456.78\nDate\tDetails\tAmount\n03NOV13\tRepaid\t\250\n"
but how do I create one string array with everthing up to the first newline and another array that holds everything after?
Then the second one will have to be split again into several string arrays so I can write out a mini-statement with the account details, then showing the transactions for each account.
I want to be able to take the original string and produce something like this on A5 paper:
You can use a LINQ query:
var cells = from row in input.Split('\n')
select row.Split('\t');
You can get just the first row using First() and the remaining rows using Skip(). For example:
foreach (string s in cells.First())
{
Console.WriteLine("First: " + s);
}
Or
foreach (string[] row in cells.Skip(1))
{
Console.WriteLine(String.Join(",", row));
}
The code below should do what you requested. This resulted in part1 having 5 entries and part2 having 26 entries
string input = "aa\tbbb\tccc\tddd\teee\nAccount\tType\tPersonal Current Account\tCurrency\tGBP\tBalance\t123,456.78\nDate\tDetails\tAmount\n03NOV13\tTransfer\t9,999,999.00-\n02NOV13\t\Cheque\t125.00\nDebit Card Cash\t200.00\n30OCT13\tLoan Repayment\t1,234.56-\n\tType\t30-Day Notice Savings Account\tCurrency\tGBP\tBalance\t983,456.78\nDate\tDetails\tAmount\n03NOV13\tRepaid\t\250\n";
// Substring starting at 0 and ending where the first newline begins
string input1 = input.Substring(0, input.IndexOf(#"\n"));
/* Substring starting where the first newline begins
plus the length of the new line to the end */
string input2 = input.Substring(input.IndexOf(#"\n") + 2);
string[] part1 = Regex.Split(input1, #"\\t");
string[] part2 = Regex.Split(input2, #"\\t");
my double data in text file like below:
1.4 2.3 3.4
2.2 2.5 2.5
I want simply read this data from file
and store it in an array.
please help me.
I'm a beginner in C#
You can use LINQ:
double[] numbers =
File.ReadAllLines(path)
.Select(s => double.Parse(s)
.ToArray()
If each line can have multiple numbers, you'll need to split the lines:
double[] numbers =
File.ReadAllLines(path)
.SelectMany(s => s.Split(' '))
.Select(s => double.Parse(s)
.ToArray()
You can also use a normal loop:
List<double> numbers = new List<double>();
foreach(string line in File.ReadAllLines(path)) {
numbers.Add(Double.Parse(line));
}
Or, to split them,
List<double> numbers = new List<double>();
foreach(string line in File.ReadAllLines(path)) {
foreach(string word in line.Split(' ') {
numbers.Add(Double.Parse(word));
}
}
Consult MSDN with the following pointers...
File class to read the file contents as string
String.Split to split the numbers based on your delimited
and Double.Parse to convert from string into double.
var values = from value in File.ReadAllText("C:\\Yourfilename.txt").Split(' ')
select double.Parse(value);
Try this, it should be somewhat more bulletproof than some of the other answers. It does not check for invalid data entries which can't be parsed correctly.
var doubles = File.ReadAllText(path)
.Split(new[] {" ", "\r\n", "\n"}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => double.Parse(s, CultureInfo.GetCultureInfo("en-US"))).ToArray();
This will work if the doubles are splitted by either a space or a line break. And it also works if there are multiple spaces/line breaks. I live in DK so I have setted the culture info explicit for parsing.