The code below adds the characters \r\n to my string variable but once the string is returned the Newline is ignored.
Here is a snippet of the returned string: Mondavi\r\nrms_processtype
And here is the code where I add a Newline:
char[] charsToTrim = { ',', ' ' };
feed = feed.TrimEnd(charsToTrim) + Environment.NewLine;
Here's the code that error's when it attempts to read the "feed" variable
var dict = feed.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None)
.SelectMany(s => s.Split('|')).ToDictionary(t => t.Split(',')[0], t => t.Split(',')[1]);
Try StringSplitOptions.RemoveEmptyEntries
Related
I have to copy and paste excel into a form and extract each value and since it's separated by tabs I'm doing something like:
var lines = PasteText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
foreach (var currLine in lines.Where(x => !String.IsNullOrWhiteSpace(x)))
{
var items = currLine.Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries)
.Take(15).ToArray();
}
But a problem arises if there's a tab inside one of the column fields.
How could I go about solving this?
check this code:
string t = #"\nazerty \n\nazerty \n\nazerty \nazerty";
string[] firstMethod = t.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);
string[] secondMethod = t.Split(new string[]{#"\n"}, StringSplitOptions.RemoveEmptyEntries);
why does first method NOT work and second does ???
Thx
This isn't working because you are using verbatim strings, i.e.:
string t = #"\nazerty \n\nazerty \n\nazerty \nazerty";
... is equivalent to:
string t = "\\nazerty \\n\\nazerty \\n\\nazerty \\nazerty";
It's likely that you actually wanted the following, which uses newline characters instead of literal backslash-n:
string t = "\nazerty \n\nazerty \n\nazerty \nazerty";
This would be "successfully" split on either new[] { "\n" } or new[] { '\n' } (but not new[] { #"\n" } which expects backslash-backslash-n).
I want to ask you about the below code:
string[] seledCats = new string[0];
string condsCats = EzCoding.Web.UI.QueryStringParsing.GetValue(
"CondsCats",
EzCoding.Web.RequestMethod.Post);
if (condsCats != null)
{
seledCats = condsCats.Split(new string[] { "," },
StringSplitOptions.RemoveEmptyEntries);
}
After insert the selected data in the array list, output like that A1,A2,
But I want to show it like that this one 'A1','A2'
So, How can i do it ?
Thanks.
You can use this little LINQ query:
string condsCats = EzCoding.Web.UI.QueryStringParsing.GetValue("CondsCats",EzCoding.Web.RequestMethod.Post);
string[] seledCats = null;
if(condsCats != null)
seledCats = condsCats
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => String.Format("'{0}'", s))
.ToArray();
seledCats = condsCats.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string s = "'" + string.Join("','", seledCats) + "'";
//to split into array again...
seledCats = condsCats.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
Here is my code :
var query = System.IO.File.ReadLines(#"c:\temp\mytext.txt")
.Select(line => line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0]);
System.IO.File.WriteAllLines(#"c:\temp\WriteLines.txt", query);
I want to convert quert to string so I can edit it , like this code for example
string[] res = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
I can use res[0].Substring and res[0].PadRight many options in text
So how can I convert my first code to become string but do same function?
I'm assuming that your file looks like this (Like grid):
Text11 Text12 Text13 Text14
Text21 Text22 Text23 Text24
Text31 Text32 Text33 Text34
Now you want to split it by cells so:
var query = System.IO.File.ReadLines(#"c:\temp\mytext.txt")
.Select(line => line.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries)).ToList();
Now when you writing this back to the file you should join the lines:
System.IO.File.WriteAllLines(#"c:\temp\WriteLines.txt", query.Select(line => String.Join(" ", line)));
And when you want to edit cell you can use:
query[row][column].Substring();
You can simply add .ToArray() at the end :
var query = System.IO.File.ReadLines(#"c:\temp\mytext.txt")
.Select(line => line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0])
.ToArray();
So you can do like query[0].Substring and query[0].PadRight.
Is that what you want?
I am using the following code to read a tab-delimited stream.
using (StreamReader readFile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readFile.ReadLine()) != null)
{
row = line.Split('\t');
parsedData.Add(row);
}
}
However, occasionally a user may supply a space-separated or comma-separated file. How do I automatically detect the delimiter instead of having to change row = line.Split('\t'); to row = line.Split(' '); or row = line.Split(',');?
Thanks.
You can use to string.Split method to split your data by number of characters
var delims = new [] {',', '\t', ' ' };
var result = line.Split(delims, StringSplitOptions.RemoveEmptyEntries);
Or you can use Regex
var result = Regex.Split(line, #"[,\t ]+")
You can't differentiate between them before hand.
What you can do is try to split on all of them:
row = line.Split('\t', ' ', ',');
This of course assumes that the data between delimiters doesn't contain the delimiters.
You'll have to define what a separator is and how you detect it. If you say: "The separator for a file is the first non-quoted whitespace character I encounter on the first line", then you can read the first line and determine the separator. You can then pass that to the .Split() method.
row = line.Split(new char[]{' ', ',', '\t'}, StringSplitOptions.RemoveEmptyEntries);