Last active
October 30, 2019 20:19
-
-
Save lockworld/9344b7ce0935f7d12faf9766d5157932 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
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