Sunday, December 4, 2011

ten must have C# string manipulations

Well, actually here are nine string manipulations you cannot live without and one you will never need. These tests passed for me:
  1. Determine if a string contains another string:

    [TestMethod]

    public void ShouldFindLiarInBilliards()

    {

       string billiards = "billiards";

       string liar = "liar";

       int matchNotation = billiards.IndexOf(liar);

       bool isMatch;

       if (matchNotation != -1)

       {

          isMatch = true;

       } else {

          isMatch = false;

       }

       Assert.AreEqual(true,isMatch);

    }


     

     
  2. Split a string into an array of strings based upon a particular character:

    [TestMethod]

    public void BilliardsShouldSplitIntoThreePieces()

    {

       string billiards = "billiards";

       string splitChar = "i";

       string[] splitPieces = billiards.Split(splitChar.ToCharArray());

       Assert.AreEqual(3, splitPieces.Length);

       Assert.AreEqual("b", splitPieces[0]);

       Assert.AreEqual("ll", splitPieces[1]);

       Assert.AreEqual("ards", splitPieces[2]);

    }


     

     
  3. Drop the last character on a string:

    [TestMethod]

    public void MayMakeBilliardsSingular()

    {

       string billiards = "billiards";

       string billiard = billiards.TrimEnd('s');

       Assert.AreEqual("billiard",billiard);

    }


     

     
  4. Trim the last few characters and/or the first few characters off of a string:

    [TestMethod]

    public void MayTrimBilliardsToLiarWithSubstring()

    {

       string billiards = "billiards";

       billiards = billiards.Substring(0, 7);

       Assert.AreEqual("billiar",billiards);

       billiards = billiards.Substring(3, 4);

       Assert.AreEqual("liar",billiards);

    }


     

     
  5. Use regular expressions for determining if a string is of a particular shape:

    [TestMethod]

    public void BilliardsIsNotAnEmailAddress()

    {

       string billiards = "billiards";

       bool isMatch;

       if (Regex.IsMatch(billiards, @"^\w+([-+.']\w+)*@\w+([-+.']\w+)*\.\w+([-+.']\w+)*$"))

       {

          isMatch = true;

       } else {

          isMatch = false;

       }

       Assert.AreEqual(false, isMatch);

    }


     

     
  6. Convert a string to title case:

    [TestMethod]

    public void MayCastSentenceToTitleCase()

    {

       string billiards = "Come to Tom's billiards room!";

       CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

       TextInfo textInfo = cultureInfo.TextInfo;

       billiards = textInfo.ToTitleCase(billiards);

       Assert.AreEqual("Come To Tom's Billiards Room!", billiards);

    }


     

     
  7. Replace a piece of a string with other characters:

    [TestMethod]

    public void MayReplaceLiarWithTruthTeller()

    {

       string billiards = "billiards";

       billiards = billiards.Replace("liar", "truth-teller");

       Assert.AreEqual("biltruth-tellerds", billiards);

    }


     

     
  8. Encrypt a string:

    [TestMethod]

    public void MayEncryptBilliards()

    {

       string billiards = "billiards";

       System.Security.Cryptography.MD5CryptoServiceProvider objCript;

       objCript = new System.Security.Cryptography.MD5CryptoServiceProvider();

       byte[] bytes = System.Text.Encoding.UTF8.GetBytes(billiards);

       bytes = objCript.ComputeHash(bytes);

       System.Text.StringBuilder builder = new System.Text.StringBuilder();

       foreach (byte bit in bytes)

       {

          builder.Append(bit.ToString("x2").ToLower());

       }

       billiards = builder.ToString();

       Assert.AreEqual("dcffcb328215e331284aacb75781da68", billiards);

    }


     

     
  9. Cast a double and/or a decimal to a string while formatting it as currency:

    [TestMethod]

    public void MayFormatBilliardsHallDailyRevenueAsCurrency()

    {

       decimal decimalValue = 1921.39m;

       string decimalString = String.Format("Day: {0:C}", decimalValue);

       Assert.AreEqual("Day: $1,921.39", decimalString);

       double doubleValue = 1921.39;

       string doubleString = String.Format("Day: {0:C}", doubleValue);

       Assert.AreEqual("Day: $1,921.39", doubleString);

       Assert.AreEqual(decimalString,doubleString);

    }


     

     
  10. Reverse a string (which you will never need to do):

    [TestMethod]

    public void MayReverseBilliards()

    {

       string billiards = "billiards";

       char[] characterArray = billiards.ToCharArray();

       Array.Reverse(characterArray);

       billiards = new string(characterArray);

       Assert.AreEqual("sdraillib",billiards);

    }


     

     
For more information on string manipulations, please click the image here:

No comments:

Post a Comment