HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Function to split either a string array or string into a string array

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayintofunctioneithersplitstring

Problem

I am programming a language interpreter in C# and have recently created a set of functions that receive either a string or a string[] and split it by a received string.

For example, with a string:

"Hey:123:hello:456"
":"


It will return this array:

{"hey","123","hello","456"}


And with a string[]:

{"a:b","c","d:e:f"}
":"


It will return this array:

{"a","b","c","d","e","f"}


How can I improve efficiency and maybe make it tidier?

public string[] SplitRows(object thevar, string delimiter) {

            if (delimiter == "") delimiter = "\n";

            if (thevar.GetType() == typeof(string)) {
                string temp = (string) thevar;
                return temp.Split(new string[] {
                    delimiter
                }, System.StringSplitOptions.None);
            }
            if (thevar.GetType() == typeof(string[])) {
                return stringarraysplitter((string[]) thevar, delimiter);
            }
            return null;
        }

        public string[] stringarraysplitter(string[] arr, string delimiter) {
            string[][] tempr = new string[arr.Length][];
            for (int i = 0; i  templist = new System.Collections.Generic.List  ();
            for (int i = 0; i < tempr.Length; i++) {
                for (int j = 0; j < tempr[i].Length; j++) {
                    templist.Add(tempr[i][j]);
                }
            }

            return templist.ToArray();
        }


Side notes:

  • SplitRows() is the only main function allowed to receive either a string array or string. Even though I use 2 functions, I cannot create 2 for the 2 variable types.



  • ` is a replacement string for \n` in my language and can be ignored.

Solution

if (delimiter == "") delimiter = "\n";


Why do you need this? Why not use normal strings?

if (thevar.GetType() == typeof(string)) {


What's wrong with normal method overloading?

if (thevar.GetType() == typeof(string)) {...}
if (thevar.GetType() == typeof(string[])) {...}
return null;


If arguments is not valid throw an ArgumentException. Don't say "I'll just return null, and see where it will go wrong."

temp.Split(new string[] {delimiter}, System.StringSplitOptions.None);


This bit repeats, and should be extracted into a method.

for (int i = 0; i < tempr.Length; i++) {
    for (int j = 0; j < tempr[i].Length; j++) {


Use Linq to simplify loop operations.

Here is the cleaned up version:

public IEnumerable Split(string str, string delimiter)
    {
        return str.Split(new[]{delimiter}, StringSplitOptions.None);
    }

    public IEnumerable Split(IEnumerable arr, string delimiter) {
        return arr.SelectMany(s => Split(s, delimiter));
    }

Code Snippets

if (delimiter == "<newline>") delimiter = "\n";
if (thevar.GetType() == typeof(string)) {
if (thevar.GetType() == typeof(string)) {...}
if (thevar.GetType() == typeof(string[])) {...}
return null;
temp.Split(new string[] {delimiter}, System.StringSplitOptions.None);
for (int i = 0; i < tempr.Length; i++) {
    for (int j = 0; j < tempr[i].Length; j++) {

Context

StackExchange Code Review Q#114618, answer score: 4

Revisions (0)

No revisions yet.