Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
namespace MadWorldApps.PCL.Helpers.Extensions
{
public static class StringExtensions
{
/// <summary>
/// Adds spaces to words based on capital characters.
/// </summary>
/// <param name="s">The CamelCase string you want spaces added to.</param>
/// <returns>A clean string with spaces added between CamelCase words.</returns>
public static string AddSpacesToWord(this string s)
{
if (String.IsNullOrEmpty(s)) return String.Empty;
var str = s;
for (var i = 0; i < str.Length; i++)
{
if (!Char.IsUpper(str[i]) || i == 0) continue;
str = str.Insert(i, " ");
//incremement the counter because we just added a space
//in front of the upper case character
i++;
}
return str;
}
/// <summary>
/// Creates a string with all spaces removed.
/// </summary>
/// <param name="s">The string you want to remove spaces from.</param>
/// <returns>A string with all words squished together.</returns>
public static string RemoveSpacesFromWords(this string s)
{
return s.Replace(" ", "");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.