Skip to content

Instantly share code, notes, and snippets.

@zoint
Created February 4, 2014 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoint/8798403 to your computer and use it in GitHub Desktop.
Save zoint/8798403 to your computer and use it in GitHub Desktop.
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