I need to read a string with non-space separated values (0-9).
Why can't I use Empty literal in String.Split method?
// Reading Grid's row and col size
gridInputValues = Console.ReadLine().Split(' ');
gridRow = int.Parse(gridInputValues[0]);
gridCol = int.Parse(gridInputValues[1]);
gridMatrix2D = new int[gridRow, gridCol];
// Reading Grid's row * col matrix values
for( int r = 0; r < gridRow; r++ )
{
//string[] inputVal = Console.ReadLine().Split('');
//string[] inputVal = Console.ReadLine().Split(string.Empty));
string inputVal = Console.ReadLine();
for( int c = 0; c < gridCol; c++ )
{
//gridMatrix2D[r, c] = int.Parse(inputVal[c]);
gridMatrix2D[r, c] = int.Parse(inputVal[c].ToString());
}
Why not,
string[] inputVal = Console.ReadLine().Split('');
or
string[] inputVal = Console.ReadLine().Split(string.Empty));
works?
Alternatively,
Is using string.ToString good practice in such case?
or
Will the string.ToString method on each iteration increase the running time?
EDIT 1:
Input:
"12345" // type is string
Expected Output:
"1","2","3","4","5" // type is string[]
What about:
Console.ReadLine().ToArray()
You donĀ“t seem to need split the string, you just want the individual characters.
or String.ToCharArray as #Tim Schmelter correctly pointed out.
You can probably try this
string[] inputVal = Console.ReadLine().Split(null);
or
string[] inputVal = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
You can instead use chars:
int[] intArray = inputVal.Select(ch => ch-'0').ToArray();
Related
I am new to c# and I don't understand why this isn't working. I want to split a previously splitted string.
My code is the following:
int i;
string s;
string[] temp, temp2;
Console.WriteLine("write 'a-a,b-b,c-c,d-d'";
s = Console.ReadLine();
temp = s.Split(',');
for (i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
I get the following error Cannot implicitly convert type 'string[]' to 'string
I want to end with:
temp = {a-a , b-b , c-c , d-d};
temp2 = {{a,a},{b,b},{c,c},{d,d}};
The result of string.Split() is string[], which you should already see by the correct usage when you assign to string[] temp. But when you are assigning to the elements of string[] temp2, you are trying to store arrays of strings in slots that are meant to only store strings, hence the compiler error. Your code could work with a simple change below.
string[] temp;
string[][] temp2; // array of arrays
string s = "a-a,b-b,c-c,d-d";
temp = s.Split(',');
temp2 = new string[temp.Length][];
for (int i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
When you call split, it returns an array of strings. You can't assign string[] to a string variable.
As others have said, Split returns an array. However, you can split on more than one character at a time. For example,
string s = "a,b,c,d-d";
var split = s.Split(new[] {',', '-'});
In this case, the split array would contain 5 indices, containing "a", "b", "c", "d", and "d".
I have char [] like {'Q','W','E','a','b','c','A','B','C'}
i want to put this char [] start from index 3 {'1',2','3'}
the result need to be {'Q','W','E','1',2','3''A','B','C'}
how can i do that please?
thanks
It could be done using Linq as follows.
string[] Op1 = {"a","f","h","x","k","w","7"};
string[] Op2 = {"1",2","3"};
int StartIndex = 3;
string[] Result = Op1.Take(StartIndex).Concat(Op2).ToArray();
I've tried several things and just cannot seem to get this. I have 10 MotorReply strings that I then split into array elements. Then I want to copy those elements to another array so I can loop through again but whatever I try, I can't access the BayReplyArray by using the incrementing i variable, i.e. BayReplyArray[i]
Declarations:
string[] MotorReplyArray = new string[30];
string[] BayReplyArray1 = new string[30];
string[] BayReplyArray2 = new string[30];
string[] BayReplyArray3 = new string[30];
up to 10
int j = 0;
for (int i = 1; i < 11; i++)
{
// here we take the Motor? reply string for each bay and split the parameters into individual string arrays
char[] delimiters = new char[] { '\r', ':' };
MotorReplyArray = MotorReply[i].Split(delimiters);
foreach (string line in MotorReplyArray)
{
// trim whitespace from ends
MotorReplyArray[j] = line.Trim();
j++;
}
Array.Copy(MotorReplyArray, BayReplyArray[i], j);
Array.Clear(MotorReplyArray, 0, j);
j = 0;
}
I can't access the BayReplyArray by using the incrementing i variable, i.e. BayReplyArray[i]
You seem to think that if i is 1 then BayReplyArray[i] is the same as BayReplyArray1, which is not the case. You can easily enough change to a jagged array:
string[] MotorReplyArray = new string[30];
string[][] BayReplyArray = new string[][10];
now BayReplyArray[i] is a string array and you can use Array.Copy on it.
I am new to c# and I don't understand why this isn't working. I want to split a previously splitted string.
My code is the following:
int i;
string s;
string[] temp, temp2;
Console.WriteLine("write 'a-a,b-b,c-c,d-d'";
s = Console.ReadLine();
temp = s.Split(',');
for (i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
I get the following error Cannot implicitly convert type 'string[]' to 'string
I want to end with:
temp = {a-a , b-b , c-c , d-d};
temp2 = {{a,a},{b,b},{c,c},{d,d}};
The result of string.Split() is string[], which you should already see by the correct usage when you assign to string[] temp. But when you are assigning to the elements of string[] temp2, you are trying to store arrays of strings in slots that are meant to only store strings, hence the compiler error. Your code could work with a simple change below.
string[] temp;
string[][] temp2; // array of arrays
string s = "a-a,b-b,c-c,d-d";
temp = s.Split(',');
temp2 = new string[temp.Length][];
for (int i = 0; i < temp.Length; i++)
temp2[i] = temp[i].Split('-');
When you call split, it returns an array of strings. You can't assign string[] to a string variable.
As others have said, Split returns an array. However, you can split on more than one character at a time. For example,
string s = "a,b,c,d-d";
var split = s.Split(new[] {',', '-'});
In this case, the split array would contain 5 indices, containing "a", "b", "c", "d", and "d".
What is the best way to create the matrix C?
string A;
char[] B = A.ToCharArray();
string[] C = new string[B.Length];
for (int i = 0; i < B.Length ; i++)
{
C[i] = B[i].ToString();
}
You just want a nicer way to do what you're doing? I suppose you could do it like this:
string A = "ABCDEFG";
string[] C = A.Select(c => c.ToString()).ToArray();
Another option as well as mquander's is to use Array.ConvertAll():
string[] C = Array.ConvertAll(A.ToCharArray(), c => c.ToString());
I generally prefer the LINQ approach, but ConvertAll is worth knowing about (for both arrays and lists) as it's able to use the fact that it knows the size to start with.
using System.Text.RegularExpressions;
string[] chars = Regex.Split(s, string.Empty);