Example #1
0
        public static string ToCase(string input, StringCasing casing, CasingOptions options = CasingOptions.Default)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            switch (casing)
            {
            case StringCasing.Unchanged:
                return(input);

            case StringCasing.Lower:
                return(input.ToLowerInvariant());

            case StringCasing.Upper:
                return(input.ToUpperInvariant());

            case StringCasing.Proper:
                return(ToProperCase(input, options));

            case StringCasing.Sentence:
                return(ToSentenceCase(input, options));

            default:
                throw new ArgumentOutOfRangeException(nameof(input));
            }
        }
Example #2
0
        public static string ToProperCase(string input, CasingOptions options = CasingOptions.Default)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            // TextInfo.ToTitleCase preserves sequences of all-caps, assuming that the sequence represents an acronym.
            // If we do not wish to preserve acronyms, we'll make the entire string lowercase first.

            if (!options.HasFlag(CasingOptions.PreserveAcronyms))
            {
                input = input.ToLowerInvariant();
            }

            string result = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input);

            // Fix possessive S (e.g. "'s") because TextInfo.ToTitleCase will capitalize them (e.g. "John's" -> "John'S").

            result = Regex.Replace(result, @"\b(['’])S\b", "$1s");

            if (options.HasFlag(CasingOptions.CapitalizeRomanNumerals))
            {
                result = CapitalizeRomanNumerals(result);
            }

            return(result);
        }
Example #3
0
 public CustomStringNormaliserWithSillyConstructorArgument(string casing)
 {
     if (casing == "UPPER")
     {
         _casing = CasingOptions.UpperCase;
     }
     else if (casing == "lower")
     {
         _casing = CasingOptions.UpperCase;
     }
     else
     {
         throw new ArgumentOutOfRangeException(nameof(casing));
     }
 }
        public static string ToSentenceCase(string input, CasingOptions options = CasingOptions.Default)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (!options.HasFlag(CasingOptions.PreserveAcronyms))
            {
                input = input.ToLowerInvariant();
            }

            string result = Regex.Replace(input, @"(?:^|[\.!?])\s*(\w)", m => m.Value.ToUpperInvariant());

            if (options.HasFlag(CasingOptions.CapitalizeRomanNumerals))
            {
                result = CapitalizeRomanNumerals(result);
            }

            return(result);
        }
Example #5
0
        public static string ToSentenceCase(string input, CasingOptions options, SentenceCasingOptions sentenceCasingOptions)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(input);
            }

            if (!options.HasFlag(CasingOptions.PreserveAcronyms))
            {
                input = input.ToLowerInvariant();
            }

            string result = input;

            if (sentenceCasingOptions.HasFlag(SentenceCasingOptions.DetectMultipleSentences))
            {
                // Detect multiple sentences in the same string, and convert them all to sentence case.

                string pattern = sentenceCasingOptions.HasFlag(SentenceCasingOptions.RequireWhitespaceAfterPunctuation) ?
                                 @"(?:^|[\.!?]\s)\s*(\w)" :
                                 @"(?:^|[\.!?])\s*(\w)";

                result = Regex.Replace(input, pattern, m => m.Value.ToUpperInvariant());
            }
            else
            {
                // Treat the entire string as a single sentence (only capitalize the first letter).

                result = Regex.Replace(input, @"^\s*(\w)", m => m.Value.ToUpperInvariant());
            }

            if (options.HasFlag(CasingOptions.CapitalizeRomanNumerals))
            {
                result = CapitalizeRomanNumerals(result);
            }

            return(result);
        }
Example #6
0
        // Public members

        public CaseConverter(StringCasing casing, CasingOptions options = CasingOptions.Default)
        {
            this.casing  = casing;
            this.options = options;
        }
Example #7
0
 public static string ToSentenceCase(string input, CasingOptions options)
 {
     return(ToSentenceCase(input, options, SentenceCasingOptions.Default));
 }
Example #8
0
 public PrivatePropertyCaseEnforcingStringNormaliser(CasingOptions casing)
 {
     _casing = casing;
 }
 public static string ToProper(this string input, CasingOptions options = CasingOptions.Default)
 {
     return(StringUtilities.ToProperCase(input, options));
 }
Example #10
0
 public static string ToProper(this string input, CasingOptions options = CasingOptions.Default)
 {
     return(CaseConverter.ToProperCase(input, options));
 }