Console.ReadLine().Split (',') does not work [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the simple snippet of my C# project below.
char[] Delimiters = new char[] { ',' };
string[] Input = Console.ReadLine ().Split (Delimiters);
Console.WriteLine (Input[0], Input[1]);
I only seem to be getting Input[0]. I've checked on Microsoft's page for splitting strings and various other sources and from what I can tell this SHOULD work.

Your Console.WriteLine method is incorrect. There is no overload that takes in multiple strings and outputs them all individually. Instead, you can format or manually concatenate the strings and pass that into the WriteLine method.
Console.WriteLine("{0}, {1}", Input[0], Input[1]);

Try writing it to separate lines
using System.Linq;
char[] Delimiters = new char[] { ',' };
string[] Input = Console.ReadLine().Split(Delimiters, StringSplitOptions.RemoveEmptyEntries);
Input.ToList().ForEach(Console.WriteLine);

Related

Remove '\' from a string reading from a CSV file - C# [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm running into a problem where I'm reading a CSV file and I don't see any '\' in the file.
When I split the string by ',' and store it into a string variable, this is what the string looks like.
"\"3040063816\""
The whole entire string is this before spliting
"\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r"
How do you remove '\' from the string?
Because when I try to convert/parse the number into an INT, it gives me an error.
I've tried to replace(#"\", string.empty) and it doesn't work.
Try the following. This should get you going.
string input = "\"3040063816\",\"123456789\",\"0.00\",\"0.00\",\"-95.99\",\"10/28/19\",\"09:04:11\",\"1\"\r";
List<string> stringArr = input.Split(',').Select(x => x.Replace(#"""", "")).ToList();
Console.WriteLine(Convert.ToInt64(stringArr[0])); // This is a large number. Has to be int64.
Console.WriteLine(int.Parse(stringArr[1]));
Console.WriteLine(float.Parse(stringArr[2]));
Console.WriteLine(float.Parse(stringArr[3]));
Console.WriteLine(float.Parse(stringArr[4]));
Console.WriteLine(DateTime.Parse(stringArr[5]));
Console.WriteLine(DateTime.Parse(stringArr[6]));
Console.WriteLine(int.Parse(stringArr[7]));
Output
3040063816
123456789
0
0
-95.99
10/28/2019 12:00:00 AM
12/30/2019 9:04:11 AM
1
Each of the strings inside your string has double quotes in it. To remove that and make it a number representation, remove the quotes from each of the string after splitting it.
Review this post for conversion of large numbers and which format they can be converted to.

System.Array Doesn't contain a Definition for Length [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I try to calculate the number of words in a sentence using a Windows console application. But I'm new to the array thing and don't really know what can I do with that code:
string sentence;
Console.WriteLine("Enter your sentence:");
sentence = Console.ReadLine();
string [] words = sentence.Split(' ');
Console.WriteLine(sentence.Lenght); // .Lenght is where I get the error from
Console.ReadKey();
In the easiest case (when word == any characters between spaces) you can implement something like this:
Console.WriteLine("Enter your sentence:");
// try not declaring local variable prematurely, but exacly where you want them
string sentence = Console.ReadLine();
// StringSplitOptions.RemoveEmptyEntries - what if user starts with space?
// separate words with two spaces, e.g. " This is a test input "
// we want 5, right?
int count = sentence
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Length; // Please, notice spelling
Console.WriteLine(count);
Console.ReadKey();
Im really dumb to not make sure if I had any spelling mistakes before I post here.Now when you spell Length word correctly,It works.Sorry guys,but,thanks.

How should I separate a string which contains slashes with a single slash? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This is a sting like this:
string a = "C:\folder1\folder2\folder3";
I want to separate string a with '\', so write like this:
List<string> result = a.Split('\\').ToList();
But, result only contains ONE member:
{C: older1 older2 older3}
I want to have 4 members in result:
{C:,folder1,folder2,folder3}
So, how shold I do it?
The problem is that your sample string does not contain backslashes.
This string contains three:
string a = "C:\\folder1\\folder2\\folder3";
or this:
string a = #"C:\folder1\folder2\folder3"; // google: verbatim string literal
\f is an escape sequence for formfeed.
Define your string as
string a = #"C:\folder1\folder2\folder3";
so it does not takes backslash as special char.

Split string is not working in WPF (C#) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When using the following code,
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split("\t", 1);
I get an error (below) on the 2nd line.
Error 1 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments MainWindow.xaml.cs 46 30
Can anyone please tell me what I'm doing wrong?
String.Split takes a char parameter, not a string. You needed to write:
string text = comboLanguages.SelectedItem.ToString();
string[] split = text.Split(new char[] {'\t'}, 1);
You need to change the code to:
string[] split = text.Split(new char[] { '\t' }, 1);
Note that the error is telling you exactly what the problem is.
If you are going to call this code regularly you might want to move the array declaration outside of the call to Split.

How to assign values to an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to assign invalid characters to an array and then call a function that checks if an entered string from a text box has the invalid characters from the array in it.
string[] invalidchars = new string[3] ("!", "#","#",)
I keep getting an error under string that says string is a class type and cannot be used as an expression
Your syntax needs a few fixes. Here's the correct version.
string[] invalidchars = new string[] { "!", "#", "#" };
Primarily, you need to use { } instead of ( ).
You just need to replace your paranthesis with curve braces and remove comma:
string[] invalidchars = new string[3] {"!", "#", "#"};
but the shortest way to do it would be:
var invalidchars = new[] {"!", "#", "#"};
By values of array C# compiler can infer the type of array.

Categories