Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Index of Upper Case letter from a String [duplicate]

Possible Duplicate:
An algorithm to "spacify" CamelCased strings

I have a string like this: MyUnsolvedProblem

I want to modify the string like this: My Unsolved Problem

How can I do that? I have tried using Regex with no luck!

like image 836
NaveenBhat Avatar asked Aug 09 '11 14:08

NaveenBhat


People also ask

How can I get only uppercase letters in a string?

To find all the uppercase characters in a string, call the replace() method on the string passing it a regular expression, e.g. str. replace(/[^A-Z]/g, '') . The replace method will return a new string containing only the uppercase characters of the original string.

How do you find the index of a capital letter in Python?

Method #2 : Using enumerate() + isupper() In this, the indices are captured using enumerate(), and isupper() does task of uppercase check as in above method.

Can be used to return a string in upper case letters?

The java string toUpperCase() method returns the string in uppercase letter. In other words, it converts all characters of the string into upper case letter.


2 Answers

var result = Regex.Replace("MyUnsolvedProblem", @"(\p{Lu})", " $1").TrimStart();

Without regex:

var s = "MyUnsolvedProblem";
var result = string.Concat(s.Select(c => char.IsUpper(c) ? " " + c.ToString() : c.ToString()))
    .TrimStart();
like image 84
Kirill Polishchuk Avatar answered Sep 30 '22 14:09

Kirill Polishchuk


resultString = Regex.Replace("MyUnsolvedProblem", "([a-z])([A-Z])", "$1 $2");
like image 38
ipr101 Avatar answered Sep 30 '22 14:09

ipr101