uniblab icon

fixing retarded string code

uniblab | PRO | 09/07/18 03:32:02 PM UTC | 0 ⭐ | 1362 👁️ | Never ⏰ | []
C# |

1.29 KB

|

None

|

0 👍

/

0 👎

// original, brain-dead retarded
public static string CommaListToSingleQuotedCommaList( string commaList ) {
    string result = "";
    try {
        do {
            if ( string.IsNullOrEmpty( commaList ) )
                break;
            string[] commaListItems = commaList.Split( "," );
            if ( ( commaListItems == null ) || ( commaListItems.Count() <= 0 ) )
                break;
            string quotedList = "";
            foreach ( string commaListItem in commaListItems ) {
                quotedList = quotedList + ( !string.IsNullOrEmpty( quotedList ) ? "," : "" ) + "'" + commaListItem.Trim() + "'";
            }
            result = quotedList;
        } while ( false );
    } catch {
        result = "";
    }
    return result;
}
 
 
// fixed
public static System.String TrimToNull( this System.String @string ) {
    if ( System.String.IsNullOrEmpty( @string ) ) {
        return null;
    }
    @string = @string.Trim();
    if ( System.String.IsNullOrEmpty( @string ) ) {
        return null;
    }
    return @string;
}
public static System.String CommaListToSingleQuotedCommaList( System.String commaList ) {
    commaList = commaList.TrimToNull();
    if ( System.String.IsNullOrEmpty( commaList ) ) {
        return null;
    }
    return System.String.Join(
        ",",
        commaList.Split( new System.Char[ 1 ] { ',' },
        System.StringSplitOptions.RemoveEmptyEntries
    ).Select(
        x => "'" + x.TrimToNull() + "'"
    ) );
}

Comments