/// <summary>
        ///  Returns a list of CharacterCasingCheck objects that are utilized when a CharacterCasingFormatting rule is applied to a property.
        /// </summary>
        public static CharacterCasingChecks GetChecks()
        {
            if (_getChecksSource != null)
            {
                return(_getChecksSource.Invoke());
            }

            if (_instance == null)
            {
// ReSharper disable UseObjectOrCollectionInitializer
                _instance = new CharacterCasingChecks();
// ReSharper restore UseObjectOrCollectionInitializer
                //TODO - developers - you can load this from a data base, config file, web service, etc.
                //  See SetGetChecksSource below.  BBQ Shack uses SetGetChecksSource.
                //
                //You can also add, remove or edit these by modifying the CharacterCasingChecks collection from your application.
                //
                //These are values that are specific to your company or line of business
                //remove the ones that don't apply and add your own.
                //ensure that the lengths of the LookFor and the ReplaceWith strings are the same
                _instance.Add(new CharacterCasingCheck("Po Box", "PO Box"));
                _instance.Add(new CharacterCasingCheck("C/o ", "c/o "));
                _instance.Add(new CharacterCasingCheck("C/O ", "c/o "));
                _instance.Add(new CharacterCasingCheck("Vpn ", "VPN "));
                _instance.Add(new CharacterCasingCheck("Xp ", "XP "));
                _instance.Add(new CharacterCasingCheck(" Or ", " or "));
                _instance.Add(new CharacterCasingCheck(" And ", " and "));
                _instance.Add(new CharacterCasingCheck(" Nw ", " NW "));
                _instance.Add(new CharacterCasingCheck(" Ne ", " NE "));
                _instance.Add(new CharacterCasingCheck(" Sw ", " SW "));
                _instance.Add(new CharacterCasingCheck(" Se ", " SE "));
                _instance.Add(new CharacterCasingCheck(" Llc. ", " LLC. "));
                _instance.Add(new CharacterCasingCheck(" Llc ", " LLC "));
                _instance.Add(new CharacterCasingCheck(" Lc ", " LC "));
                _instance.Add(new CharacterCasingCheck(" Lc. ", " LC. "));
                _instance.Add(new CharacterCasingCheck("Wpf", "WPF"));
            }

            return(_instance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Corrects the text character casing and optionally format phone fields simliar to Microsoft Outlook.
        /// </summary>
        /// <param name="strIn">String to be case corrected and optionally formatted.</param>
        /// <param name="characterCase">Character case and format.</param>
        /// <returns>String case corrected and optionally formatted.</returns>
        public static String ApplyCharacterCasing(String strIn, CharacterCasing characterCase)
        {
            strIn = strIn.Trim();

            if (strIn.Length == 0)
            {
                return(strIn);
            }

            Int32 intX;

            switch (characterCase)
            {
            case CharacterCasing.None:
                return(strIn);

            case CharacterCasing.LowerCase:
                return(strIn.ToLower());

            case CharacterCasing.UpperCase:
                return(strIn.ToUpper());

            case CharacterCasing.OutlookPhoneNoProperName:
                return(FormatOutLookPhone(strIn));

            case CharacterCasing.OutlookPhoneUpper:
                return(FormatOutLookPhone(strIn).ToUpper());
            }

            strIn = strIn.ToLower();

            String strPrevious      = " ";
            String strPreviousTwo   = "  ";
            String strPreviousThree = "   ";
            String strChar;

            for (intX = 0; intX < strIn.Length; intX++)
            {
                strChar = strIn.Substring(intX, 1);

                if (char.IsLetter(Convert.ToChar(strChar)) && strChar != strChar.ToUpper())
                {
                    if (strPrevious == " " || strPrevious == "." || strPrevious == "-" || strPrevious == "/" || strPreviousThree == " O'" || strPreviousTwo == "Mc")
                    {
                        strIn       = strIn.Remove(intX, 1);
                        strIn       = strIn.Insert(intX, strChar.ToUpper());
                        strPrevious = strChar.ToUpper();
                    }
                    else
                    {
                        strPrevious = strChar;
                    }
                }
                else
                {
                    strPrevious = strChar;
                }

                strPreviousTwo   = strPreviousTwo.Substring(1, 1) + strPrevious;
                strPreviousThree = strPreviousThree.Substring(1, 1) + strPreviousThree.Substring(2, 1) + strPrevious;
            }

            intX = strIn.IndexOf("'");

            if (intX == 1)
            {
                String strInsert = strIn.Substring(2, 1).ToUpper();
                strIn = strIn.Remove(2, 1);
                strIn = strIn.Insert(2, strInsert);
            }

            try {
                intX = strIn.IndexOf("'", 3);

                if (intX > 3 && strIn.Substring(intX - 2, 1) == " ")
                {
                    String strInsert = strIn.Substring(intX + 1, 1).ToUpper();
                    strIn = strIn.Remove(intX + 1, 1);
                    strIn = strIn.Insert(intX + 1, strInsert);
                }

// ReSharper disable EmptyGeneralCatchClause
            } catch {
// ReSharper restore EmptyGeneralCatchClause
            }

            //never remove this code
            strIn += " ";

            foreach (CharacterCasingCheck check in CharacterCasingChecks.GetChecks())
            {
                if (strIn.Contains(check.LookFor))
                {
                    Int32 intPosition = strIn.IndexOf(check.LookFor);

                    if (intPosition > -1)
                    {
                        strIn = strIn.Remove(intPosition, check.LookFor.Length);
                        strIn = strIn.Insert(intPosition, check.ReplaceWith);
                    }
                }
            }

            if (characterCase == CharacterCasing.OutlookPhoneProperName)
            {
                strIn = FormatOutLookPhone(strIn);
            }

            return(strIn.Trim());
        }