Created
February 4, 2014 05:02
-
-
Save zoint/8798403 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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