String Helper class provides basic text manipulation functions

Namespace:  CA.Common.Text
Assembly:  CA.Common (in CA.Common.dll) Version: 1.0.0.0 (1.0.0.0)

Remarks

String Helper class provides basic text manipulation functions, the most notable of the functions are
CopyFull Source Code for the StringHelper class
//Source code from the Code Associate C# code library, Full documentation and latest updates can be found
//@ http://www.codeassociate.com/caapi/

using System;
using System.Text.RegularExpressions;

namespace CA.Common.Text
{
    public static class StringHelper
    {

-        #region SplitQuotedString
         // used internally as a marker as to where the code is when splitting the text
         private enum LineIndexFieldState
         {
             StartField = 1,            // Start of a field 
             EndQuote = 2,            // We have a End Quote can only be found in InQuoteFieldDate
             Delimiter = 3,            // We are currently at a delimiter 
             InFiledData = 4,        // We are in copy of data and looking for a delimiter 
             InQuoteFieldDate = 5,    // We are in Quoted field and looking for an End Quote 
         }
 
         private static bool IsCurrentCharADelimiter(char testChar, char[] Delimiters)
         {
             bool result = false;
             foreach(char delimeter in Delimiters)
             {
                 if (delimeter == testChar)
                 {
                     result = true;
                     break;
                 }
             }
             return result;
         }
 
         public static StringCollection SplitQuotedString(char[] Delimiters, char Quote, string input)
         {
             LineIndexFieldState state = LineIndexFieldState.StartField;
             System.Text.StringBuilder FieldData = new System.Text.StringBuilder();
             StringCollection result = new StringCollection();
 
             char[] datalineArray = input.ToCharArray();
             for (int i = 0; i < datalineArray.Length; i++)
             {
                 char CurrentChar = datalineArray[i];
                 switch (state)
                 {
-                    #region LineIndexFieldState.StartField
                     case LineIndexFieldState.StartField:
                         {
                             if (CurrentChar == Quote)
                                 state = LineIndexFieldState.InQuoteFieldDate;
                             else if (IsCurrentCharADelimiter(CurrentChar, Delimiters))
                                 state = LineIndexFieldState.Delimiter;
                             else
                             {
                                 // it was not a quote or a delimiter so state is InFiledData
                                 state = LineIndexFieldState.InFiledData;
                                 FieldData.Append(CurrentChar);
                             }
                             break;
                         }
                     #endregion
-                    #region LineIndexFieldState.InFiledData
                     case LineIndexFieldState.InFiledData:
                         {
                             // We are in the field data and looking for a delimiter 
                             if (IsCurrentCharADelimiter(CurrentChar, Delimiters))
                                 state = LineIndexFieldState.Delimiter;
                             else
                                 FieldData.Append(CurrentChar);
                             break;
                         }
                     #endregion
-                    #region LineIndexFieldState.InQuoteFieldDate:
                     case LineIndexFieldState.InQuoteFieldDate:
                         {
                             // We are in the field data and looking for an End Quote..
                             if (CurrentChar == Quote)
                             {
                                 if (i != datalineArray.Length - 1) // if true we are at end of line so ignore
                                 {
                                     if (datalineArray[i + 1] == Quote) // if the next char is also a quote 
                                     {
                                         // if should be a single quote so add a single quote and inc the counter
                                         FieldData.Append(CurrentChar);
                                         i++;
                                     }
                                     else // it is the end of the delimter 
                                     {
                                         state = LineIndexFieldState.EndQuote;
                                     }
                                 }
                             }
                             else
                             {
                                 FieldData.Append(CurrentChar);
                             }
 
                             break;
                         }
                     #endregion
-                    #region LineIndexFieldState.EndQuote:
                     case LineIndexFieldState.EndQuote:
                         {
                             // we have hit the end quote so look for the delimiter and ignore all until delimiter is hit..
                             if (IsCurrentCharADelimiter(CurrentChar, Delimiters))
                                 state = LineIndexFieldState.Delimiter;
                             break;
                         }
                     #endregion
                 }
-                #region LineIndexFieldState.Delimiter:
                 if (state == LineIndexFieldState.Delimiter)
                 {
                     result.Add(FieldData.ToString());
                     // Clear the existing Field and create a container to populate the data..
                     FieldData = new System.Text.StringBuilder();
                     state = LineIndexFieldState.StartField;
                 }
                 #endregion
             }
             // Automatic end of line code so add the last field.
             result.Add(FieldData.ToString());
             // Copies the collection to a new array starting at index 0.
             return result;
         }
 
 
 
         public static StringCollection SplitQuotedString(char Delimiter, char Quote, string input)
         {
             // create a single array to hold the single delimiter
             char[] Delimiters = new char[1];
             Delimiters[0] = Delimiter;
             // call the overloaded fucntion
             return SplitQuotedString(Delimiters, Quote, input);
         }
 
         public static StringCollection SplitQuotedString(char Delimiter, string input)
         {
             return SplitQuotedString(Delimiter, '"', input);
         }
 
         #endregion

-        #region ParsePascalOrCamelString
         public static StringCollection ParsePascalOrCamelString(string input)
         {
             StringCollection result = new StringCollection();
 
             if (input != null)
             {
                 if (input.Length == 0)
                     result.Add(string.Empty);
                 else
                 {
                     int wordStartIndex = 0;
                     char[] letters = input.ToCharArray();
                     // Skip the first letter. we don't care what case it is.
                     for (int i = 1; i < letters.Length; i++)
                     {
                         if (char.IsUpper(letters[i]))
                         {
                             //Grab everything before the current index.
                             result.Add(new string(letters, wordStartIndex, i - wordStartIndex));
                             wordStartIndex = i;
                         }
                     }
                     //We need to have the last word.
                     result.Add(new string(letters, wordStartIndex, letters.Length - wordStartIndex));
                 }
             }
             return result;
         }
         #endregion
    }

}

Inheritance Hierarchy

System..::.Object
  CA.Common.Text..::.StringHelper

See Also