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".
Related
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 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();
I have a object of type string,I want to convert it to String array
here the code is
obj.QueryString =HttpContext.Current.Request.Url.PathAndQuery;
string[] arr =obj.QueryString;
QueryString is of type string.
You can directly access the each index within the string. For example
string value = "Dot Net Perls";
char first = value[0];
char second = value[1];
char last = value[value.Length - 1];
// Write chars.
Console.WriteLine("--- 'Dot Net Perls' ---");
Console.Write("First char: ");
Console.WriteLine(first);
Console.Write("Second char: ");
Console.WriteLine(second);
Console.Write("Last char: ");
Console.WriteLine(last);
Output
--- 'Dot Net Perls' ---
First char: D
Second char: o
Last char: s
a string is nothing more then an array of chars, so if you want to split up the strings letters into a different string array seperatly you could do something like this:
string myString = "myString";
string[] myArray = new string[myString.Length];
for(int i = 0; i < myString.Length; i++)
{
myArray[i] = myString[i].ToString();
}
or Char Array:
string theString = "myString";
char[] theStringAsArray = theString.ToCharArray();
Insert whatever character you want to split on instead of the "&" argument in the Split method call.
obj.QueryString =HttpContext.Current.Request.Url.PathAndQuery;
string[] arr =obj.QueryString.Split(new char[] {'&'});
maybe you want to convert to char[] array instead string[] array. to do this use char[] arr = obj.QueryString.ToCharArray()
Here, this will make an array that may or may not fit your criteria.
var myArray = (from x in obj.QueryString select x.ToString()).ToArray()
You can do this compactly with Linq (similar to A.R.'s answer), but I can't speak to how efficient it is.
using System.Linq;
string input = "abcde";
var output = input.Select(char.ToString).ToArray();
> output
string[5] { "a", "b", "c", "d", "e" }
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".