I have a string like this “HelloWorldMyNameIsCarl” and I want it to become something like “Hello_World_My_Name_Is_Carl”. How can I do this?
I have a string like this HelloWorldMyNameIsCarl and I want it to become something
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, regular expressions can do that for you:
The expression
[A-Z]will match every upper case letter and put it into the second group. You need the first group.to avoid replacing the first ‘H’.As Piligrim pointed out, this solution does not work for arbitrary languages. To catch any uppercase letter defined by the Unicode stardard we need the Unicode 4.1 subproperty
\p{Lu}which matches all uppercase letters. So the more general solution looks likeThanks Piligrim.