public static string ConvertCase(this string phrase, StringAliasCaseType cases) { var helper = ShortStringHelper; var legacy = helper as LegacyShortStringHelper; var cases2 = cases.ToCleanStringType() & CleanStringType.CaseMask; return(legacy != null ? legacy.LegacyConvertStringCase(phrase, cases2) : helper.CleanString(phrase, CleanStringType.Ascii | CleanStringType.Alias | cases2)); }
/// <summary> /// Gets the CleanStringType value corresponding to the StringAliasCaseType value. /// </summary> /// <param name="aliasCaseType">The value.</param> /// <returns>A CleanStringType value corresponding to the StringAliasCaseType value.</returns> public static CleanStringType ToCleanStringType(this StringAliasCaseType aliasCaseType) { switch (aliasCaseType) { case StringAliasCaseType.PascalCase: return(CleanStringType.PascalCase); case StringAliasCaseType.CamelCase: return(CleanStringType.CamelCase); //case StringAliasCaseType.Unchanged: default: return(CleanStringType.Unchanged); } }
/// <summary> /// Converts the phrase to specified convention. /// </summary> /// <param name="phrase"></param> /// <param name="cases">The cases.</param> /// <returns>string</returns> public static string ConvertCase(this string phrase, StringAliasCaseType cases) { var splittedPhrase = Regex.Split(phrase, @"[^a-zA-Z0-9\']", RegexOptions.Compiled); if (cases == StringAliasCaseType.Unchanged) { return(string.Join("", splittedPhrase)); } //var splittedPhrase = phrase.Split(' ', '-', '.'); var sb = new StringBuilder(); foreach (var splittedPhraseChars in splittedPhrase.Select(s => s.ToCharArray())) { if (splittedPhraseChars.Length > 0) { splittedPhraseChars[0] = ((new String(splittedPhraseChars[0], 1)).ToUpper().ToCharArray())[0]; } sb.Append(new String(splittedPhraseChars)); } var result = sb.ToString(); if (cases == StringAliasCaseType.CamelCase) { if (result.Length > 1) { var pattern = new Regex("^([A-Z]*)([A-Z].*)$", RegexOptions.Singleline | RegexOptions.Compiled); var match = pattern.Match(result); if (match.Success) { result = match.Groups[1].Value.ToLower() + match.Groups[2].Value; return(result.Substring(0, 1).ToLower() + result.Substring(1)); } return(result); } return(result.ToLower()); } return(result); }
/// <summary> /// Converts a string for use with an entity alias which is camel case and without invalid characters /// </summary> /// <param name="phrase">The phrase.</param> /// <param name="caseType">By default this is camel case</param> /// <param name="removeSpaces">if set to <c>true</c> [remove spaces].</param> /// <returns></returns> public static string ToUmbracoAlias(this string phrase, StringAliasCaseType caseType = StringAliasCaseType.CamelCase, bool removeSpaces = false) { if (string.IsNullOrEmpty(phrase)) { return(string.Empty); } //convert case first var tmp = phrase.ConvertCase(caseType); //remove non-alphanumeric chars var result = Regex.Replace(tmp, @"[^a-zA-Z0-9\s\.-]+", "", RegexOptions.Compiled); if (removeSpaces) { result = result.Replace(" ", ""); } return(result); }