Skip to content

Instantly share code, notes, and snippets.

@lockworld
Last active October 30, 2019 20:19
Show Gist options
  • Save lockworld/9344b7ce0935f7d12faf9766d5157932 to your computer and use it in GitHub Desktop.
Save lockworld/9344b7ce0935f7d12faf9766d5157932 to your computer and use it in GitHub Desktop.
public static string SplitStringByCase(this string str)
{
if (!string.IsNullOrWhiteSpace(str))
{
return Regex.Replace(str, "[A-Z0-9]((?![A-Z0-9].)|(?<![A-Z0-9].))", " $0").Trim();
}
else
{
return "";
}
}
/*
Ignores groupings of capital letters and numbers
(?! is negative look-ahead, and (?<! is negative look-behind...
so this checks to see if either
* next character is not a capital or number
or
* previous character is not a capital or number
So, if either the next or the previous character is not a capital and not a number, add a space
EXAMPLES:
SimpleText => "Simple Text"
One => "One"
ABCCorporation => "ABC Corporation"
IAmNotHere => "I Am Not Here"
MixedStringBBCStructure => "Mixed String BBC Structure"
Numbers123CauseManyProblemsIKnow => "Numbers 123 Cause Many Problems I Know"
Practice online at: https://regex101.com/
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment