1

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[]
Community
  • 1
  • 1
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
  • 1
    Because Empty != whitespace. – blfuentes Sep 28 '15 at 09:01
  • What should such a split return? Where SHOULD it split if you don´t provide a delimier? On every character? On every second? Furthermore: What is your inout-string that you read? – MakePeaceGreatAgain Sep 28 '15 at 09:01
  • 1
    What does splitting a string by empty string mean? Isn't that equal to calling `string.ToCharArray()` or just enumerating the string? – Sriram Sakthivel Sep 28 '15 at 09:02
  • You can access a char in a string already like if the string was a `char[]`: `char c = inputVal[c]`. No need to create an array. – Tim Schmelter Sep 28 '15 at 09:05
  • If I use character Array, I need to call ToString() each time before parsing it to interger. I think it'll increase my running time? Isn't it? – Naveen Kumar V Sep 28 '15 at 09:08
  • 1
    possible duplicate of [Best way to specify whitespace in a String.Split operation](http://stackoverflow.com/questions/6111298/best-way-to-specify-whitespace-in-a-string-split-operation) – Nikita Shrivastava Sep 28 '15 at 09:09
  • Please update the question with example input and expected output of the `Split('')` – freedomn-m Sep 28 '15 at 09:13
  • Actually, my code works fine. I want to know if it's a good practice to split? and why not empty string literal works.? – Naveen Kumar V Sep 28 '15 at 09:21

3 Answers3

5

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.

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • If I use it, I still need to use string.ToString method at parsing, which may increase the running time. – Naveen Kumar V Sep 28 '15 at 09:20
  • 1
    @NaveenKumarV dont worry ToString will not affect performance that much. It wont be the bottleneck part of your code. Even for million ToString calls should not affect that much – M.kazem Akhgary Sep 28 '15 at 09:34
  • @Akhgary Thank you for your info on my performance doubt. :) Any idea about Why empty literal is not allowed in split method? – Naveen Kumar V Sep 28 '15 at 09:42
  • @NaveenKumarV in fact the split method will search inside string and will perform split if it find any matches inside string. The search method should access the elements of string like `"example"[i]`. So It access the elements of string just like arrays. The problem is you cant get empty string or empty char for any given `i`. empty string is non existence and you cant get a non existing thing. – M.kazem Akhgary Sep 28 '15 at 09:52
  • 1
    Also this can be the reason why empty char is not allowed. `''`. Because it does not exist. If you assume it exist then it will be all over your string. Consider `"Hello"[1]`. It should give you character `'e'` or `''`? Thats the problem. I hope you understand what i mean @NaveenKumarV – M.kazem Akhgary Sep 28 '15 at 10:02
  • @Akhgary :). Yeah. This seems reasonable. Anyway thanks for your effort to make me understand. :) – Naveen Kumar V Sep 28 '15 at 10:06
2

You can probably try this

string[] inputVal = Console.ReadLine().Split(null);

or

string[] inputVal = Console.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries)
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • It's for white Space. But I want each character in a string. For example, "12345" should be splitted as "1","2","3","4","5". – Naveen Kumar V Sep 28 '15 at 09:18
1

You can instead use chars:

int[] intArray = inputVal.Select(ch => ch-'0').ToArray();
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95