String Split Utility Method Problem with No Separators

I have the following method that I created that works fine if the corresponding separator actually exists. I want to save this from LINQ for now ...

eg.

If I pass the line "123; 322; 323", it works fine.

But if I pass only one string value without a separator, such as "123", it is clearly not going to separate it, since there is no separator. I'm just trying to find the best way to check and consider this and be able to spit out this one value 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, it seems to work, but sure for a long time

    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;
    }
+5
source share
7 answers

, .

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; 
}

. , , , , , ... ?

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;
}
+2

:

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

To:

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

, String.Split , :

// stringToSplit does not contain the splitDelimiter
string[] values = stringToSplit.Split(splitDelimiter);
// values is a string array containing one value - stringToSplit
+4

, . Split .

, , :

if (!stringToSplit.Contains(splitDelimiter))
{
    int i;
    if (Int32.TryParse(stringToSplit, out i))
        list.Add(i);
    return list;
}
+1

, string.Split() , :

If this instance does not contain any of the strings in separator, the returned array consists of a single element that contains this instance.

msdn ""

+1

, ?

yield return , .

, TryParse:

public delegate bool Parser<T>(string input, out T value);

, , :

// Notice: extension methods must belong to a class marked static.
public static class EnumerableParser
{
    // modified code to prevent horizontal overflow
    public static IEnumerable<T> ParseAll<T>
    (this IEnumerable<string> strings, Parser<T> parser)
    {
        foreach (string str in strings)
        {
            T value;
            if (parser(str, out value))
                yield return value;
        }
    }
}

StringToList :

public List<int> StringToList(string stringToSplit, char delimiter)
{
    // Notice: since string.Split returns a string[], and string[] implements
    // IEnumerable<string>, so the ParseAll extension method can be called on it.
    return stringToSplit.Split(delimiter).ParseAll<int>(int.TryParse).ToList();
}
+1

, .

public static List<int> StringToList(string stringToSplit, char splitDelimiter)
{
    int i;
    return stringToSplit.Split(splitDelimiter)
        .Where(str => int.TryParse(str, out i))
        .Select(str => int.Parse(str))
        .ToList();
}
0

, , .

0

All Articles