src/MvcContrib.FluentHtml/StringExtensions.cs
/// <summary>
/// Converts pascal case string into a phrase.  Treats consecutive capital 
/// letters as a word.  For example, 'HasABCDAcronym' would be tranformed to
/// 'Has ABCD Acronym'
/// </summary>
/// <param name="s">A string containing pascal case words</param>
Converts pascal case string into a phrase.  Treats consecutive capital
letters as a word.  For example, 'HasABCDAcronym' would be tranformed to
'Has ABCD Acronym'
Converts pascal case string into a phrase . Treats consecutive capital letters as a word . For example ' HasABCDAcronym ' would be tranformed to ' Has ABCD Acronym '
public static string PascalCaseToPhrase(this string s)
		{
			var result = new StringBuilder();
			var letters = s.ToCharArray();
			for (var i = 0; i < letters.Length; i++)
			{
				var isFirstLetter = i == 0;
				var isLastLetter = i == letters.Length;
				var isCurrentLetterUpper = Char.IsUpper(letters[i]);
				var isPrevLetterLower = i > 0 && Char.IsLower(letters[i - 1]);
				var isNextLetterLower = i + 1 < letters.Length && Char.IsLower(letters[i + 1]);
				var isPrevCharIsSpace = i > 0 && letters[i - 1] == ' ';
				if (!isFirstLetter &&
					isCurrentLetterUpper &&
					!isPrevCharIsSpace &&
					(isPrevLetterLower || isNextLetterLower || isLastLetter))
				{
					result.Append(" ");
				}
				result.Append(letters[i].ToString());
			}
			return result.ToString();
		}
public static string PascalCaseToPhrase ( this string s ) { var result = new StringBuilder ( ) ; var letters = s . ToCharArray ( ) ; for ( var i = 0 ; i < letters . Length ; i ++ ) { var isFirstLetter = i == 0 ; var isLastLetter = i == letters . Length ; var isCurrentLetterUpper = Char . IsUpper ( letters [ i ] ) ; var isPrevLetterLower = i > 0 && Char . IsLower ( letters [ i - 1 ] ) ; var isNextLetterLower = i + 1 < letters . Length && Char . IsLower ( letters [ i + 1 ] ) ; var isPrevCharIsSpace = i > 0 && letters [ i - 1 ] == ' ' ; if ( ! isFirstLetter && isCurrentLetterUpper && ! isPrevCharIsSpace && ( isPrevLetterLower || isNextLetterLower || isLastLetter ) ) { result . Append ( " " ) ; } result . Append ( letters [ i ] . ToString ( ) ) ; } return result . ToString ( ) ; }
Converts pascal case string into a phrase.
Converts pascal case string into a phrase .
string
this
s
A string containing pascal case words
A string containing pascal case words