/// <summary>
        /// Creates a acronym for the specified text and returns result.
        /// Removes all non alphabetical characters and forms an acronym from all principle words.
        /// </summary>
        /// <param name="text">Input text.</param>
        /// <param name="culture">Language culture to retrive and identify words.</param>
        /// <param name="onlyPrincipalWords">Use only principal words boolean, default true. Otherwise include all words.</param>
        /// <returns>An acronym for the input text.</returns>
        /// <remarks>Definition of acronym - an abbreviation formed from the initial letters of other words and pronounced as a word (e.g. ASCII, NASA).</remarks>
        public static string ToAcronym(this string text, CultureInfo culture, bool onlyPrincipalWords = true)
        {
            //Verify text parameter
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            //Verify culture parameter
            if (culture == null)
            {
                throw new ArgumentNullException(nameof(culture));
            }

            //Verify culture info data and set principle word handling
            onlyPrincipalWords = onlyPrincipalWords && CultureInfoData.InitializeCultureData(culture);

            //Remove all non alphabetical characters, but preserve whitespace
            var result = text.ToAlphabetic();

            //Handle all words
            result = Regex.Replace(result, @"\w+", new MatchEvaluator(WordEvaluator), RegexOptions.IgnoreCase);

            //Return result without whitespace
            return(result.RemoveWhitespace());

            //Internal evaluator
            string WordEvaluator(Match word)
            {
                //Init
                var lower = word.Value.ToLower();

                //Evaluate word
                return(string.IsNullOrWhiteSpace(word.Value) ||
                       (onlyPrincipalWords && (CultureInfoData.InfoData.data?.Articles?.Contains(lower) ?? false) ||
                        (CultureInfoData.InfoData.data?.Conjunctions?.Contains(lower) ?? false) ||
                        (CultureInfoData.InfoData.data?.Prepositions?.Contains(lower) ?? false))
                    ? string.Empty : word.Value.Substring(0, 1).ToUpper());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts string to title case with culture specific handling of articles, conjunctions and prepositions.
        /// </summary>
        /// <param name="text">Input text.</param>
        /// <param name="culture">Language culture to retrive and identify words.</param>
        /// <returns>Input text with word casing based on title case rules.</returns>
        /// <remarks>
        /// Rules:
        /// - Capitalize the first word and the last word of the title.
        /// - Capitalize the principal words.
        /// - Capitalize all words of four letters or more.
        /// - Do not capitalize articles, conjunctions, and prepositions of three letters or fewer.
        /// </remarks>
        public static string ToTitleCase(this string text, CultureInfo culture)
        {
            //Verify text parameter
            if (string.IsNullOrWhiteSpace(text))
            {
                return(text);
            }

            //Verify culture info data
            if (!CultureInfoData.InitializeCultureData(culture))
            {
                return(culture.TextInfo.ToTitleCase(text));
            }
            else
            {
                //Run first pass for base casing
                var result = culture.TextInfo.ToTitleCase(text);

                //Handle all culture Articles, Conjunctions & Prepositions
                return(Regex.Replace(result, @"\w+", new MatchEvaluator(WordEvaluator), RegexOptions.IgnoreCase));
            }

            //Internal evaluator
            string WordEvaluator(Match word)
            {
                //Init
                var lower = word.Value.ToLower();

                //Evaluate word
                var lastWord = !word.NextMatch().Success;

                return(word.Index > 0 && !lastWord && word.Value.Length <= 3 &&
                       (CultureInfoData.InfoData.data?.Articles?.Contains(lower) ?? false) ||
                       (CultureInfoData.InfoData.data?.Conjunctions?.Contains(lower) ?? false) ||
                       (CultureInfoData.InfoData.data?.Prepositions?.Contains(lower) ?? false)
                    ? lower : word.Value);
            }
        }