This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 3 months ago.
string candidates;
string[] candidatesSplit = { };
string line;
int countLines = 0;
StreamReader sr = new StreamReader("..\\..\\..\\candidates.txt"); // Read candidates from file
candidates = sr.ReadToEnd();
sr.Close();
candidatesSplit = candidates.Split(','); // Split the file with ','
Console.WriteLine(candidatesSplit[30]);
Using this code, I wanted to split every ',' and get specific words out from my text file.
My candidates file looks like this:
100,Esra Tarak,90,D1,D4,D2,A,B,D,C, ,C,A,D,B,C,D,B,A, ,B,A,C,D,C,D,A,D,B,C,D
101,Cem Ak,84,D1,D5, ,A,C,D,C,C,C,A,C,B,C,D,B,A,C,B,A,C,D,C,C,A,D,B,C,D
Code works perfectly for the first line in candidates.txt, however when it comes to the second line on the text file, the output comes out like this:
D
101
I need it to show only like this
101
I cannot put a ',' at the end of my lines. Is there any way to fix this?
Just Split(Environment.NewLine) on the entire input first and then Split(',') again on each line.
using var sr = new StreamReader("..\\..\\..\\candidates.txt");
var candidates = sr.ReadToEnd();
foreach (var line in candidates.Split(Environment.NewLine))
{
var candidatesSplit = line.Split(',');
Console.WriteLine(candidatesSplit[30]);
}
This question already has answers here:
Put result of String.Split() into ArrayList or Stack
(5 answers)
Closed 2 years ago.
I was wondering if there is a way to convert example:
'Hello.World' into '["Hello"]["World"]' or 'This.is.a.string' into ["This"]["is"]["a"]["string"]
I'm kinda new to C# and I was wondering if that's even possible using string formatting or something like that.
You can use the Split method like this string[] strings = String.Split("."); This will split your string per each period.
If you meant to include the single quotes (') as part of the string then:
String test = "'Hello.World'";
// Strip the first and last '
test = test.Substring(1, test.Length - 2);
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
// and add back in the single quotes
var result = $"'{String.Join("", split.Select(word => $"[\"{word}\"]"))}'";
Prints:
'["Hello"]["World"]'
If they just meant to surround your input then just:
String test = "Hello.World";
// Split on Period
String[] split = test.Split('.');
// Encapsulate each word with [" "]
var result = $"{String.Join("", split.Select(word => $"[\"{word}\"]"))}";
Prints: ["Hello"]["World"]
This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 9 years ago.
I need to split lines into parts in a big file (200MB-5GB) where lines looks like this
value1;value2;"value3;extra";value4;"value5;extra"
Line needs to be split by semicolon . Regular String.Split does not work since semicolons can be inside of the quotes.
I think regular expression would work best here especially if file has millions of lines. I appreciate any guidance or code that would help me to split.
Update:
The result I want to see for the above sample line is
value1
value2
"value3;extra"
value4
"value5;extra"
Thank you
Add a reference to Microsoft.VisualBasic and use the TextFieldParser class:
using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;
class Program
{
static void Main(string[] args)
{
using(var input = File.OpenRead("input.txt"))
using(var tfp = new TextFieldParser(input))
{
tfp.SetDelimiters(new string[] { ";" });
tfp.HasFieldsEnclosedInQuotes = true;
var fields = tfp.ReadFields();
foreach (var field in fields)
{
Console.WriteLine(field);
}
}
}
}
This question already has answers here:
Parsing CSV files in C#, with header
(19 answers)
Closed 1 year ago.
I am having string which gives data more like to csv format .
&stamp;field1;field2;field3;field4;&event
10:44:00.6100;0.000;0.000;0.000;0.000; 10:44:00.7100;23.2;0.230;411.2;0.000; 10:44:00.8100;0.000;0.000;1.022;0.000; 10:44:00.9100;8.000;0.000;232.3;0.000;
10:44:01.2100;0.000;0.000;0.000;0.000; 10:44:01.3100;23.2;0.230;411.2;0.000; 10:44:01.5100;0.000;0.000;1.022;0.000; 10:44:01.7100;8.000;0.000;232.3;0.000;
I want to deserialize this data.
this will give you a list of strings "split" at every ; char. you will want to trim and parse the values after. hope this helps
var stringOfData = "0:44:00.6100;0.000;0.000;0.000;0.000; 10:44:00.7100;23.2;0.230;411.2;0.000; 10:44:00.8100;0.000;0.000;1.022;0.000; 10:44:00.9100;8.000;0.000;232.3;0.000;";
List<string> parsed = new List<string>();
parsed.AddRange(stringOfData.Split(';'));
string line = String.Empty;
string[] parts = null;
using (StreamReader sr = new StreamReader(new FileStream(#"C:\yourfile.csv",
FileMode.Open)))
{
while ((line = sr.ReadLine()) != null)
{
parts = line.Split(new [] { ';' });
//Do whatever you want to do with the array of values here
}
}
As for the deserialization part of the question; you would need to specify the kind of format you would want. If you just want a collection of numbers you want to loop through, you could just add every value to a generic List.
If you want a specific format, you could create a class with fields for each of those values and populate a generic list of that class using those values.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# how to convert File.ReadLines into string array?
I have a text file that I want to make a String array from. The file is newline delimited. For example,
entry1
entry2
entry3
will make an array of {"entry1", "entry2", "entry3"}
EDIT: I am wanting to do this using DownloadString in WebClient
So you're just trying to split a string into an array that is delimited with a new line?
If so, this should work:
string temp = webClient.DownloadString(someurl);
string[] lines = temp.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Good luck.