2

I have a grid. When I enter some numeric values in that grid, it is saved in array.

                RegArrayLoopEL = RegArrayEL[a].Split('ô');

Now In case an array has empty string "" I have to replace it with 0 One way that I can do is

                if (RegArrayLoopEL[1] == "")
                {
                    RegArrayLoopEL[1] = "0";
                }

But for that I will have to use a lot of if conditions for every array. Is there any alternative for it? or any other way this can be done?

1 Answer 1

2

You can do it on assignment:

RegArrayLoopEL = RegArrayEL[a].Split('ô').Select(str => str == "" ? "0" : str).ToArray();

You could create a function or delegate to clean it up a bit and make it so if you change the logic you just need to change it one place:

string[] ReplaceEmptyStrings(IEnumerable<string> strings) =>
    strings.Select(str => str == "" ? "0" : str).ToArray();

And then your assignments would be:

RegArrayLoopEL = ReplaceEmptryStrings(RegArrayEL[a].Split('ô'));
Sign up to request clarification or add additional context in comments.

Comments

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.