Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string by capital letters [duplicate]

Tags:

string

c#

Possible Duplicate:
Regular expression, split string by capital letter but ignore TLA

I have a string which is a combination of several words, each word is capitalized.
For example: SeveralWordsString

Using C#, how do I split the string into "Several Words String" in a smart way?

Thanks!

like image 888
Nir Avatar asked Dec 20 '10 10:12

Nir


People also ask

How do you split a string with capital letters?

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 return an array of the substrings.

How do you split text into separate columns by capital letters in Excel?

You can find the Split Columns > By Uppercase to Lowercase option in three places: Home tab—under the Split Column dropdown menu inside the Transform group. Transform tab—under the Split Column dropdown menu inside the Text Column group. Right-click a column—inside the Split Column option.

How do you split words with capital letters 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.

How do I split a string into multiple parts?

Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.


1 Answers

Use this regex (I forgot from which stackoverflow answer I sourced it, will search it now):

 public static string ToLowercaseNamingConvention(this string s, bool toLowercase)         {             if (toLowercase)             {                 var r = new Regex(@"                 (?<=[A-Z])(?=[A-Z][a-z]) |                  (?<=[^A-Z])(?=[A-Z]) |                  (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);                  return r.Replace(s, "_").ToLower();             }             else                 return s;         } 

I use it in this project: http://www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html

[EDIT]

I found it now: How do I convert CamelCase into human-readable names in Java?

Nicely split "TodayILiveInTheUSAWithSimon", no space on front of " Today":

using System; using System.Text.RegularExpressions;  namespace TestSplit {     class MainClass     {         public static void Main (string[] args)         {             Console.WriteLine ("Hello World!");                var r = new Regex(@"                 (?<=[A-Z])(?=[A-Z][a-z]) |                  (?<=[^A-Z])(?=[A-Z]) |                  (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);               string s = "TodayILiveInTheUSAWithSimon";             Console.WriteLine( "YYY{0}ZZZ", r.Replace(s, " "));         }     } } 

Output:

 YYYToday I Live In The USA With SimonZZZ 
like image 183
Michael Buen Avatar answered Oct 08 '22 01:10

Michael Buen