Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Split Utility Method Problem when No delimiters are included

Tags:

c#

I've got the following method I created which works fine if there actually is the delimiter in question. I want to keep this out of LINQ for now...

e.g.

If I pass in the string "123;322;323" it works great.

But if I only pass in one string value without the delimiter such as "123" it obviously is not going to split it since there is no delimiter. I am just trying to figure out the best way to check and account for this and be able to spit out that one value back in the list

public static List<int> StringToList(string stringToSplit, char splitDelimiter)
{
    List<int> list = new List<int>();

    if (string.IsNullOrEmpty(stringToSplit))
        return list;

    string[] values = stringToSplit.Split(splitDelimiter);

    if (values.Length < 1)
        return list;

    foreach (string s in values)
    {
        int i;
        if (Int32.TryParse(s, out i))
            list.Add(i);
    }

    return list;
}

UPDATED: This is what I came up with that seems to work but sure is long

    public static List<int> StringToList(string stringToSplit, char splitDelimiter)
    {
        List<int> list = new IntList();

        if (string.IsNullOrEmpty(stringToSplit))
            return list;

        if (stringToSplit.Contains(splitDelimiter.ToString()))
        {
            string[] values = stringToSplit.Split(splitDelimiter);

            if (values.Length <= 1)
                return list;

            foreach (string s in values)
            {
                int i;
                if (Int32.TryParse(s, out i))
                    list.Add(i);
            }
        }
        else if (stringToSplit.Length > 0)
        {
            int i;
            if(Int32.TryParse(stringToSplit, out i))
                list.Add(i);
        }

        return list;
    }
like image 685
PositiveGuy Avatar asked Jun 29 '10 13:06

PositiveGuy


2 Answers

Change this condition:

if (values.Length <= 1)
    return list;

To:

if (values.Length <= 0)
    return list;

This works because String.Split will return the original string if it can't find the delimiter:

// stringToSplit does not contain the splitDelimiter
string[] values = stringToSplit.Split(splitDelimiter);
// values is a string array containing one value - stringToSplit
like image 89
Oded Avatar answered Sep 22 '22 21:09

Oded


There are a LOT of unnecessary checks for conditions that don't matter to the core logic of the method.

public static List<int> StringToList(string stringToSplit, char splitDelimiter) 
{ 
    List<int> list = new IntList(); 

    if (string.IsNullOrEmpty(stringToSplit)) 
        return list; 

    //this if is not necessary. As others have said, Split will return a string[1] with the original string if no delimiter is found
    if (stringToSplit.Contains(splitDelimiter.ToString())) 
    { 
        string[] values = stringToSplit.Split(splitDelimiter); 

        //why check this? if there are no values, the foreach will do nothing and fall through to a return anyway.
        if (values.Length <= 1) 
            return list; 

        foreach (string s in values) 
        { 
            int i; 
            if (Int32.TryParse(s, out i)) 
                list.Add(i); 
        } 
    } 
    //again, this is rendered redundant due to previous comments
    else if (stringToSplit.Length > 0) 
    { 
        int i; 
        if(Int32.TryParse(stringToSplit, out i)) 
            list.Add(i); 
    } 

    return list; 
}

Try this. You hopefully have some unit tests calling this method to make sure it works...right?

public static List<int> StringToList(string stringToSplit, char splitDelimiter) 
{ 
    List<int> list = new IntList(); 

    if (string.IsNullOrEmpty(stringToSplit)) 
        return list;

    foreach(var s in stringToSplit.Split(splitDelimiter))
    {
        int i;
        if(int.TryParse(s, out i))
            list.Add(i);
    }
    return list;
}
like image 34
Andy_Vulhop Avatar answered Sep 19 '22 21:09

Andy_Vulhop