Question

I have the following code which will convert a string to TitleCase. I would like to create an exception to this, so that if the string entered, finds the exact characters in sequence, it will ignore it and proceed to convert the rest of the string. eg. if part of the string contains: ABC I want to ignore this as a rule and proceed to convert the rest of the string in TitleCase:

public string ConvertToTitleCase(string input)
    {
        char[] chars = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(input.ToLower()).ToCharArray();

        for (int i = 0; i + 1 < chars.Length; i++)
        {

            if ((chars[i].Equals('\'')) ||
                (chars[i].Equals('-')))
            {
                chars[i + 1] = Char.ToUpper(chars[i + 1]);
            }
        }
        return new string(chars);
    }

Any ideas?

Was it helpful?

Solution

This is all you need

       private string ConvertToTitleCase(string input, string ignore) {
            var lcSB = new StringBuilder();
            var lcStart = 0;
            while (lcStart < input.Length) {
                var lcNext = input.IndexOf(ignore, lcStart);
                if (lcNext < 0)
                    lcNext = input.Length;
                lcSB.Append(input.Substring(lcStart, lcNext - lcStart).ToUpper());
                if (lcNext < input.Length)
                    lcSB.Append(ignore);
                lcStart = lcNext + ignore.Length;
            }
            return lcSB.ToString();
        }

OTHER TIPS

Normally what you do in situations like this is instead of enumerating char-by-char you break your string into parts (possibly by regex, or somehow, details are unimportant), process each part separately (capitalize the first character of each part) and join everything back.

All the exceptions in this scenario are just these "parts" you don't touch.

So the algorithm is:

  • Break the string into tokens
  • Enumerate tokens and process (or ignore if it is an exception)
  • Join the tokens back to the string.

Good Luck :)

You should try something like this, using the MatchEvaluator

return Regex.Replace(input,
                     @"\S+",
                     (match) =>
                     {
                             var word = match.Value;

                             var formatException = yourListOfTitleCaseExceptions.FirstOrDefault(e => e.Trim().Equals(word, StringComparison.InvariantCultureIgnoreCase));

                                    if (formatException == null)
                                    {
                                        Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(word.ToLower())
                                    }

                                    return formatException.Trim();

                                });

This will split the words up, check the list of exceptions you have made and ignore title-casing them. If the word isn't found, it will get title-cased as expected. If an exception is found, the un-touched word will be returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top