@@ -388,12 +388,12 @@ public static string TrimOrDefault(string input) {
388
388
389
389
}
390
390
391
- public static string Unescape ( string input , UnescapeOptions options = UnescapeOptions . Default ) {
391
+ public static string Unescape ( string value , UnescapeOptions options = UnescapeOptions . Default ) {
392
392
393
- if ( string . IsNullOrEmpty ( input ) )
394
- return input ;
393
+ if ( string . IsNullOrEmpty ( value ) )
394
+ return value ;
395
395
396
- string result = input ;
396
+ string result = value ;
397
397
398
398
if ( options . HasFlag ( UnescapeOptions . RepairTextEncoding ) )
399
399
result = RepairTextEncoding ( result ) ;
@@ -426,59 +426,84 @@ public static string Unescape(string input, UnescapeOptions options = UnescapeOp
426
426
427
427
}
428
428
429
- public static bool IsNumeric ( string input ) {
429
+ public static bool IsNumeric ( string value ) {
430
430
431
- if ( string . IsNullOrWhiteSpace ( input ) )
431
+ if ( string . IsNullOrWhiteSpace ( value ) )
432
432
return false ;
433
433
434
434
// The idea behind this method is that it should return true for any string a naive observer would consider to be a number.
435
435
// By default, decimals and negative numbers are considered numeric.
436
436
437
437
// Allow a leading a sign for negative numbers, but not positive numbers.
438
438
439
- if ( input . TrimStart ( ) . StartsWith ( "+" ) )
439
+ if ( value . TrimStart ( ) . StartsWith ( "+" ) )
440
440
return false ;
441
441
442
- return IsNumeric ( input , NumberStyles . Integer | NumberStyles . AllowDecimalPoint ) ;
442
+ return IsNumeric ( value , NumberStyles . Integer | NumberStyles . AllowDecimalPoint ) ;
443
443
444
444
}
445
- public static bool IsNumeric ( string input , NumberStyles styles ) {
445
+ public static bool IsNumeric ( string value , NumberStyles styles ) {
446
446
447
- if ( string . IsNullOrWhiteSpace ( input ) )
447
+ if ( string . IsNullOrWhiteSpace ( value ) )
448
448
return false ;
449
449
450
- return double . TryParse ( input , styles , CultureInfo . InvariantCulture , out _ ) ;
450
+ return double . TryParse ( value , styles , CultureInfo . InvariantCulture , out _ ) ;
451
451
452
452
}
453
- public static string PadDigits ( string num , int numberOfDigits ) {
453
+ public static bool IsNewLine ( string value ) {
454
+
455
+ if ( string . IsNullOrEmpty ( value ) )
456
+ return false ;
457
+
458
+ return value . Equals ( "\r " ) ||
459
+ value . Equals ( "\n " ) ||
460
+ value . Equals ( "\r \n " ) ;
461
+
462
+ }
463
+ public static bool CanBeEncodedAs ( string value , Encoding encoding ) {
464
+
465
+ if ( encoding is null )
466
+ throw new ArgumentNullException ( nameof ( encoding ) ) ;
467
+
468
+ if ( string . IsNullOrEmpty ( value ) )
469
+ return false ;
470
+
471
+ byte [ ] encodedBytes = encoding . GetBytes ( value ) ;
472
+ string decodedString = encoding . GetString ( encodedBytes ) ;
473
+
474
+ return decodedString . Equals ( value , StringComparison . InvariantCulture ) ;
475
+
476
+ }
477
+
478
+ public static string PadDigits ( string numericString , int numberOfDigits ) {
454
479
455
480
// Trim all existing leading zeros.
456
481
457
- num = ( num ?? "" ) . TrimStart ( '0' ) ;
482
+ numericString = ( numericString ?? "" ) . TrimStart ( '0' ) ;
458
483
459
484
// Make sure the string contains at least one (whole) digit.
460
485
461
- if ( num . Length <= 0 || num . StartsWith ( "." ) )
462
- num = "0" + num ;
486
+ if ( numericString . Length <= 0 || numericString . StartsWith ( "." ) )
487
+ numericString = "0" + numericString ;
463
488
464
489
// Pad the string with zeros so that the leading digits have a length of /at least/ the desired number of digits.
465
490
// If there are already more leading digits than desired, no padding is added.
466
491
467
- int currentLeadingDigits = num . IndexOf ( "." ) ;
492
+ int currentLeadingDigits = numericString . IndexOf ( "." ) ;
468
493
469
494
if ( currentLeadingDigits < 0 )
470
- currentLeadingDigits = num . Length ;
495
+ currentLeadingDigits = numericString . Length ;
471
496
472
497
int paddingLength = Math . Max ( numberOfDigits - currentLeadingDigits , 0 ) ;
473
498
474
- num = "" . PadLeft ( paddingLength , '0' ) + num ;
499
+ numericString = "" . PadLeft ( paddingLength , '0' ) + numericString ;
475
500
476
- return num ;
501
+ return numericString ;
477
502
478
503
}
479
- public static string PadDigits ( int num , int numberOfDigits ) {
504
+ public static string PadDigits ( int numericString , int numberOfDigits ) {
480
505
481
- return PadDigits ( num . ToString ( CultureInfo . InvariantCulture ) , numberOfDigits ) ;
506
+ return PadDigits ( numericString . ToString ( CultureInfo . InvariantCulture ) , numberOfDigits ) ;
482
507
483
508
}
484
509
@@ -550,17 +575,6 @@ public static Stream StringToStream(string value, Encoding encoding) {
550
575
551
576
}
552
577
553
- public static bool IsNewLine ( string value ) {
554
-
555
- if ( string . IsNullOrEmpty ( value ) )
556
- return false ;
557
-
558
- return value . Equals ( "\r " ) ||
559
- value . Equals ( "\n " ) ||
560
- value . Equals ( "\r \n " ) ;
561
-
562
- }
563
-
564
578
public static string GetRandomString ( int length ) {
565
579
566
580
return GetRandomString ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" , length ) ;
0 commit comments