uniblab

fixing retarded string code

Sep 7th, 2018
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. // original, brain-dead retarded
  2. public static string CommaListToSingleQuotedCommaList( string commaList ) {
  3.     string result = "";
  4.     try {
  5.         do {
  6.             if ( string.IsNullOrEmpty( commaList ) )
  7.                 break;
  8.             string[] commaListItems = commaList.Split( "," );
  9.             if ( ( commaListItems == null ) || ( commaListItems.Count() <= 0 ) )
  10.                 break;
  11.             string quotedList = "";
  12.             foreach ( string commaListItem in commaListItems ) {
  13.                 quotedList = quotedList + ( !string.IsNullOrEmpty( quotedList ) ? "," : "" ) + "'" + commaListItem.Trim() + "'";
  14.             }
  15.             result = quotedList;
  16.         } while ( false );
  17.     } catch {
  18.         result = "";
  19.     }
  20.     return result;
  21. }
  22.  
  23.  
  24. // fixed
  25. public static System.String TrimToNull( this System.String @string ) {
  26.     if ( System.String.IsNullOrEmpty( @string ) ) {
  27.         return null;
  28.     }
  29.     @string = @string.Trim();
  30.     if ( System.String.IsNullOrEmpty( @string ) ) {
  31.         return null;
  32.     }
  33.     return @string;
  34. }
  35. public static System.String CommaListToSingleQuotedCommaList( System.String commaList ) {
  36.     commaList = commaList.TrimToNull();
  37.     if ( System.String.IsNullOrEmpty( commaList ) ) {
  38.         return null;
  39.     }
  40.     return System.String.Join(
  41.         ",",
  42.         commaList.Split( new System.Char[ 1 ] { ',' },
  43.         System.StringSplitOptions.RemoveEmptyEntries
  44.     ).Select(
  45.         x => "'" + x.TrimToNull() + "'"
  46.     ) );
  47. }
Add Comment
Please, Sign In to add comment