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.
Related
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.
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);
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
Update: The code works, error was caused by something else.
How do you find the index of a curly brace ({) in a string? I keep getting -1 even though the curly brace is there. Here's my code;
var str = "example string with {brace}.";
var index = str.IndexOf("{");
I've tried escaping the brace like this
var index = str.IndexOf("{{");
and like this
var index = str.IndexOf("{{}");
but it still returns -1
The code you've posted works just fine. Tested in LinqPad
var str = "example string with {brace}.";
var index = str.IndexOf("{");
returns 20.
There is no need to try and escape the { character. If you're for sure getting back -1 you have a different problem and will probably need to post more information/code for it to be resolved.
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.
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.