Advertisement
reapism

Most optimal way to check specific characters only appear in

Nov 10th, 2019
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. // Most optimal way to check specific characters only appear in a string
  2. // https://stackoverflow.com/questions/58786021/most-optimal-way-to-check-specific-characters-only-appear-in-a-string/58786086#58786086
  3. Every class, and call exactly in the business logic.
  4.  
  5. // My original code
  6.         public static bool ContainsOnlyChars(string strValue, params char[] charValues)
  7.         {
  8.         if (string.IsNullOrEmpty(strValue))
  9.             throw new ArgumentNullException("String cannot be null or empty.");
  10.  
  11.         for (int i = 0; i < strValue.Length; i++)
  12.         {
  13.             if (!charValues.Any(c=> c == strValue[i]))
  14.             {
  15.                 return false;
  16.             }
  17.         }
  18.  
  19.         return true;
  20.         }
  21.  
  22. // A function thats called from my test class.
  23. public class SRFIsStringIgnorableUtility
  24.     {
  25.         public static char[] IgnorableCharacters { get; }
  26.  
  27.         static SRFIsStringIgnorableUtility()
  28.         {
  29.             IgnorableCharacters = new char[] { ' ', '\f', '\n', '\r', '\t', '\v', };
  30.         }
  31.  
  32.         public static bool IsStringIgnorable(string strValue)
  33.         {
  34.             return SRFContainsUtility.ContainsOnlyChars(strValue, IgnorableCharacters);
  35.         }
  36.     }
  37.  
  38. // Test class cases
  39.         [TestCase(" ")]
  40.         [TestCase("\f")]
  41.         [TestCase("\n")]
  42.         [TestCase("\r")]
  43.         [TestCase("\t")]
  44.         [TestCase("\v")]
  45.         [TestCase("  ")]
  46.         [TestCase("\f\f")]
  47.         [TestCase("\n\n")]
  48.         [TestCase("\r\r")]
  49.         [TestCase("\t\t")]
  50.         [TestCase("\v\v")]
  51.         [TestCase(" \f")]
  52.         [TestCase(" \n")]
  53.         [TestCase(" \r")]
  54.         [TestCase(" \t")]
  55.         [TestCase(" \v")]
  56.         [TestCase("  \f")]
  57.         [TestCase("  \n")]
  58.         [TestCase("  \r")]
  59.         [TestCase("  \t")]
  60.         [TestCase("  \v")]
  61.         [TestCase("\f\f ")]
  62.         [TestCase("\n\n ")]
  63.         [TestCase("\r\r ")]
  64.         [TestCase("\t\t ")]
  65.         [TestCase("\v\v ")]
  66.         [TestCase("\f \f")]
  67.         [TestCase("\n \n")]
  68.         [TestCase("\r \r")]
  69.         [TestCase("\t \t")]
  70.         [TestCase("\v \v")]
  71.         [TestCase(" \f\f")]
  72.         [TestCase(" \n\n")]
  73.         [TestCase(" \r\r")]
  74.         [TestCase(" \t\t")]
  75.         [TestCase(" \v\v")] // all should return true because only chars in the string appear in the char array
  76.         public void ShouldReturnTrueGivenIgnorableString(string strValue)
  77.         {
  78.             Assert.IsTrue(
  79.                 SRFIsStringIgnorableUtility.IsStringIgnorable(strValue)
  80.             );
  81.         }
  82.  
  83.         [TestCase(" ;")]
  84.         [TestCase("\f;")]
  85.         [TestCase("\n;")]
  86.         [TestCase("\r;")]
  87.         [TestCase("\t;")]
  88.         [TestCase("\v;")]
  89.         [TestCase("  ;")]
  90.         [TestCase("\f\f;")]
  91.         [TestCase("\n\n;")]
  92.         [TestCase("\r\r;")]
  93.         [TestCase("\t\t;")]
  94.         [TestCase("\v\v;")]
  95.         [TestCase(" \f;")]
  96.         [TestCase(" \n;")]
  97.         [TestCase(" \r;")]
  98.         [TestCase(" \t;")]
  99.         [TestCase(" \v;")]
  100.         [TestCase("  \f;")]
  101.         [TestCase("  \n;")]
  102.         [TestCase("  \r;")]
  103.         [TestCase("  \t;")]
  104.         [TestCase("  \v;")]
  105.         [TestCase("\f\f ;")]
  106.         [TestCase("\n\n ;")]
  107.         [TestCase("\r\r ;")]
  108.         [TestCase("\t\t ;")]
  109.         [TestCase("\v\v ;")]
  110.         [TestCase("\f \f;")]
  111.         [TestCase("\n \n;")]
  112.         [TestCase("\r \r;")]
  113.         [TestCase("\t \t;")]
  114.         [TestCase("\v \v;")]
  115.         [TestCase(" \f\f;")]
  116.         [TestCase(" \n\n;")]
  117.         [TestCase(" \r\r;")]
  118.         [TestCase(" \t\t;")]
  119.         [TestCase(" \v\v;")] // all should return false because of the selicolon
  120.         public void ShouldReturnFalseGivenNonIgnorableString(string strValue)
  121.         {
  122.             Assert.IsFalse(
  123.                 SRFIsStringIgnorableUtility.IsStringIgnorable(strValue)
  124.             );
  125.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement