How to assign values to an array [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 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.

Related

System.String[] returned instead of array value [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 1 year ago.
Improve this question
I'm trying to write a very basic program to return the value of 2 elements in an array of string but the output is System.String[] instead of the actual value of the string. Is it because I have it two letters associated to 1 element. For example :
static void Main(string[] args)
{
string[] a = new string[2] { "ugly string", "lovely string" };
foreach (var item in a)
{
Console.WriteLine(a);
}
}
It should return ugly string and lovely string in two separate lines. However it's just showing System.String[] in 2 different lines.
You need to change your line
Console.WriteLine(a);
to
Console.WriteLine(item);
foreach will make sure the for each (hence the name) iteration through the list called a the current value will be put into the variable named item.

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.

Console.ReadLine().Split (',') does not work [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 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);

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.

Categories