Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string with uppercase [duplicate]

Tags:

c#

.net

Possible Duplicates:
Split a PascalCase string into separate words
is there a elegant way to parse a word and add spaces before capital letters

Is there a simple way to split this string "TopLeft" to "Top" and "Left"

like image 620
Kimtho6 Avatar asked Nov 02 '10 15:11

Kimtho6


People also ask

How do you split a string in uppercase?

To split a string on capital letters, call the split() method with the following regular expression - /(? =[A-Z])/ . The regular expression uses a positive lookahead assertion to split the string on each capital letter and returns an array of the substrings.

How do you split str?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you split a string by capital letter in Python?

findall() method to split a string on uppercase letters, e.g. re. findall('[a-zA-Z][^A-Z]*', my_str) . The re. findall() method will split the string on uppercase letters and will return a list containing the results.


1 Answers

If you want it dynamic, meaning every time you find an upper case letter break it apart, I don't believe this is built in, but could be wrong; it's easy enough to write an extension method.

string output = "";

foreach (char letter in str)
{
   if (Char.IsUpper(letter) && output.Length > 0)
     output += " " + letter;
   else
     output += letter;
}
like image 168
Brian Mains Avatar answered Oct 05 '22 17:10

Brian Mains