0

Update: July 26, 2017

I have a string inside which the values are comma separated. However for some cases it is coming double comma ,, at in a consecutive way. But when I am using using string.split(',') it's returning me a array which doesn't have a value on that index. For example

string str = "12,3,5,,6,54,127,8,,0,98," 

It's breaking down the the array this way

str2[0] = 12
str2[1] = 3
str2[2] = 5
str2[3] = ""
str2[4] = 6
str2[5] = 54
str2[6] = 127
str2[7] = 8
str2[8] = ""
str2[9] = 0
str2[10] = 98
str2[11] = ""

Look here I am getting the array with one or more empty value. So I want to put a 0 in each empty position when I am splitting the string. Here I have found something to skip the empty values

str .Split(',', StringSplitOptions.RemoveEmptyEntries) 

However I did not found such a solution put a default value at empty index. I have gone through these previous Questions Q1, Q2, But these are not effective for mine. I am using C# for web application in .Net framework

Community
  • 1
  • 1
Ananda G
  • 2,389
  • 23
  • 39
  • 2
    Possible duplicate of [Replace all occurences of a string from a string array](https://stackoverflow.com/questions/11789750/replace-all-occurences-of-a-string-from-a-string-array) – mjwills Jul 26 '17 at 11:24
  • 2
    AFAIK, there not an out of the box method that do this. But once you get your array with empty strings, you just have to iterate through it and replace the empty strings by 0. Or as suggested by mjwills, you can replace all occurence of ",," in your initial string by ",0," and then split your string. – fharreau Jul 26 '17 at 11:27
  • Well, you may say that, but I would not like to agree. the Question you referred, it was replacing form string array. However I am converting it directly from string. @mjwills – Ananda G Jul 26 '17 at 11:28
  • 2
    Notice that these two solutions require to iterate twice (either the string or the produced array). If you want to iterate your string only one time, you have to write your own loop! – fharreau Jul 26 '17 at 11:30
  • @fharreau I agree with you, that may be available in `C#` Library. for this reason I have asked. – Ananda G Jul 26 '17 at 11:31

4 Answers4

5

Try the below code:

You can able to use IEnumerable extension method (Select) of String object.

string str = "12,3,5,,6,54,127,8,,0,98";
var strVal = str.Split(',').Select(s => string.IsNullOrWhiteSpace(s) ? "0" : s);
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
4

Use the following code to replace empty string to zero

string str = "12,3,5,,6,54,127,8,,0,98";
       var a= str.Split(',').Select(x=>string.IsNullOrEmpty(x)?"0":x);
Koderzzzz
  • 849
  • 8
  • 18
2

You can run your string through regex to put zeros into it before going into Split:

Regex.Replace(str, "(?<=(^|,))(?=(,|$))", "0").Split(',')

The regex will insert zeros into the original string in spots when two commas are next to each other, or when a comma is detected at the beginning or at the end of the string (demo).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

While all the suggested solutions work perfectly, they are all iterating twice your input (once for the string split, once for the string replace or regex, once for the array replace).

Here is a solution iterating only once the input:

var input = "12,3,5,,6,54,127,8,,0,98";

var result = new List<int>();
var currentNumeric = string.Empty;
foreach(char c in input)
{
    if(c == ',' && String.IsNullOrWhiteSpace(currentNumeric))
    {
        result.Add(0);
    }
    else if(c == ',')
    {
        result.Add(int.Parse(currentNumeric));
        currentNumeric = string.Empty;
    }
    else
    {
        currentNumeric += c;
    }
}

if(!String.IsNullOrWhiteSpace(currentNumeric))
{
    result.Add(int.Parse(currentNumeric));
}
else if(input.EndsWith(","))
{
    result.Add(0);
}
fharreau
  • 2,105
  • 1
  • 23
  • 46
  • Nop, this snippet does not handle a ending comma even if it does handle leading comma (I was lucky on this :D). I can fix it easily if the OP want me to (something like `else if(input.EndsWith(",")) result.Add(0);` should do the work). – fharreau Jul 26 '17 at 12:01
  • I just add the ending comma support – fharreau Jul 26 '17 at 13:50