1

I have the following example strings:

,something2,something3,,something5
something1,,something3,something4,

I need to pull this into an array, but insert the "None" value for anything missing. (Different pieces of this string could be missing) For instance, it should be this when completed:

None,something2,something3,None,something5
something1,None,something3,something4,None

The length of the "array" would always be 5 values.

Anyone know how I would do that in C#? I can split the string into an array, but then I can't seem to update the array:

s.Split(char(',')) //How do I add the 'None' to the blank array parts?

If easier to add the "None" to the string and then split that to an array that would work as well.

4 Answers 4

4

The followig code will split the string and replace empties with None:

var str = "something1,,something2,,something4";
var srtWithNone = str.Split(',').Select(x => string.IsNullOrEmpty(x) ? "None" : x);

Make sure you import: using System.Linq;

Sign up to request clarification or add additional context in comments.

Comments

0

Below code should help you getting an output array containing your desired splitted strings in an array:

static void Main(string[] args)
{
    var s = ",something2,something3,,something5,something1,,something3,something4,";
    var splittedArray = s.Split(',');
    for (int i = 0; i < splittedArray.Length; i++)
    {
        if (splittedArray[i] == string.Empty)
        {
            splittedArray[i] = "None";
        }
    }
}

Once the above code runs then you would see that all empty parts in the array has got replaced with None as shown in the snapshot below:

enter image description here

Comments

0
var strings = new string[] { "a", "b", "c", null, "1","2", null, "zed" };

// replace the nulls with "None"
var noneStrings = strings.Select( strv => { return strv ?? "None"; });
Debug.WriteLine(String.Join(",", noneStrings) );

In the other direction:

var separators = new []{ "," };
var otherway = "a,b,c,,1,2,,zed";
var values = otherway.Split(separators, StringSplitOptions.None);

// [a,b,c,{null},1,2,{null},zed]

var fixedValues = values.Select( strv => { return String.IsNullOrEmpty(strv) ? "None" : strv; });

// [a,b,c,None,1,2,None,zed]

Should work a treat

Comments

0

Assuming each item is in the format 'something{n}' where {n} is number between 1 and 5

var str = "something1,,something3,something4,";
const int limit = 5;
var a = str.Split(',');

for (var i = 0; i < limit; i++)
{
    if (string.IsNullOrEmpty(a[i]))
    {
        a[i] = "None";
    }
}
var resultStr = string.Join(",", a);

5 Comments

This doesn't use any modern LINQ functionality
The question does not say it needs LINQ. Most of the the LINQ method internally does the same looping logic. LINQ is not magic. It is simply some optimize helper methods.
That's irrelevant. This solution is far less maintainable than it needs to be.
Not maintainable? Thats a pretty surprising claim, cmon man, this code is trivial.
Really? You've looked at the code above and you're saying that this is trivial? I mean, this might work, but it's far from ideal. If I came across this code in our code, I'd refactor straight away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.