how to convert string to string array? - c#

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" }

Related

XMLNODELIST to GenericList and Split [duplicate]

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".

Replace from start index on array C#

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();

How can I use Empty literal on Split('')

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();

How to replace every char in a string value with the next char?

If I have a string variable like this:
string f = "ABC";
I want to make it like this:
f="CDE"
This means that I want to take every char in this string and increase it to the next 2 values, if I have 'a' I want to change it to 'c' and so on.
Following will increment the character to + 2, Not sure what you want when then characters are ending character in the alphabets.
string f = "ABC";
string result = new string(f.Select(r =>(char) (r + 2)).ToArray());
For string ABC result will be CDE, but for string XYZ result will be Z[\
All you need to do is get the individual char from the string e.g.
string a = "aba";
char b = a[0]; //the value is equal to 'a'
Console.WriteLine((char)((int)b + 1));
then convert the char into an int and increament it then convert it back to a char
You can convert the string to a char[], modify each char as needed, and convert the result back to a string as follows:
char[] chars = "ABC".ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
chars[i] += (char)2;
}
string result = new string(chars);
// result == "CDE"

Cannot implicitly convert string[] to string when splitting

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".

Categories