6

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?

Chii
  • 14,540
  • 3
  • 37
  • 44
w4nderlust
  • 1,057
  • 2
  • 12
  • 22
  • Was leaving off "IsCarl" intentional or part of what you're attempting to accomplish? – Otis Oct 19 '09 at 21:02

3 Answers3

37

Yes, regular expressions can do that for you:

"HelloWorldMyNameIsCarl".replaceAll("(.)([A-Z])", "$1_$2")

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 like

"HelloWorldMyNameIsCarl".replaceAll("(.)(\\p{Lu})", "$1_$2")

Thanks Piligrim.

Community
  • 1
  • 1
Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
3

Is this homework? To get you started:

  1. Create a StringBuffer
  2. Iterate over your string.
  3. Check each character to be uppercase (java.lang.Character class will help)
  4. Append underscore to buffer if so.
  5. Append current character to buffer.
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Splitting or regex seem much more natural. – Stefan Kendall Oct 19 '09 at 21:10
  • +1 - This seems like a perfectly workable solution to me (particularly if this is homework). – Matthew Murdoch Oct 19 '09 at 21:12
  • Regex is not mentioned anywhere in the question, though it is indeed tagged thus (which I honestly haven't noticed right up until your comment). That said, doing something like this with regex is completely ridiculous. – ChssPly76 Oct 19 '09 at 21:14
  • No, it's not. Your method would require excessive documentation and preconditions to understand or test fully. I could read the regex and understand its intent immediately, and the comments would be simple. – Stefan Kendall Oct 19 '09 at 21:17
  • Regular expressions aid in pattern matching. We seem to be matching a pattern here. I find the application straightforward. – Stefan Kendall Oct 19 '09 at 21:19
  • 3
    It always makes me feel creepy when people won't just iterate over a damn list. This is the easy, natural obvious answer. Regex would be (significantly) slower, more prone to bugs and, in this case, probably less elegant/longer. Not that "Elegant" is a good measure of code except in those rare cases where it happens to coincide with "Readable" – Bill K Oct 19 '09 at 21:21
  • @Bill: Do you not use regex often? I use it all the time in personal text editors, validation, and the like, and it tends to save my life more often than not. I can only think that the regex naysayers just don't understand regex well enough to recommend it as a solution. Furthermore, speed won't be a consideration in a desktop application. I've spawned perl processes to run regex and parse text **in real time with the user's keystroke** and the performance hit was not noticeable. Matching was perceivably instantaneous, even when spawning a separate process and runtime to perform the match. – Stefan Kendall Oct 19 '09 at 21:26
  • 1
    I also use regular expression all the time but I wouldn't use it here, because a) it's homework anyway and b) you probably miss some obscure international uppercase letters (Yes, there is an uppercase i with the dot!). @Stefan Flaming does not help. Neither does screaming in bold. – Henning Oct 19 '09 at 21:48
  • Bill mentioned a performance loss; I explained why he was **wrong** for must purposes. There's no "flaming" here. And even if it was homework, it was a) tagged with regex and b) internationalization and its applications in regular expressions were not the OP's question or likely use case. – Stefan Kendall Oct 19 '09 at 22:54
2

Here's a hint to get you thinking along a possible solution:

  1. Find a way of splitting the string into parts at each capital letter
  2. Join the split strings back up with underscores between them

Useful keywords:

  • split
  • regular expression/regex
Rob
  • 47,999
  • 5
  • 74
  • 91
  • 1
    This is a living proof of how great this quote really is: http://stackoverflow.com/questions/58640/great-programming-quotes/58646#58646 :-) – ChssPly76 Oct 19 '09 at 21:11
  • What do you have against regular expressions, ChssPly76? – Stefan Kendall Oct 19 '09 at 21:22
  • String.split removes the matched (=splitting) characters and is therefore not really helpful here. – Henning Oct 19 '09 at 21:34
  • Some people, when confronted with a problem, think, "I know, I'll steal a quote from jwz". Now they have two problems. More seriously though, I don't care how the OP does the splitting; the point I was trying to make was to encourage him/her to break down the problem a little and find solutions to smaller problems. You can farm out the split-at-uppercase to a pack of trained monkeys if you so wish. – Rob Oct 19 '09 at 21:44
  • 1
    @Rob - I didn't steal anything, I've ...umm... quoted the quote :-) And I totally agree with your point; I just don't think regular expressions are the best way to solve this particular problem. That said, OP did tag this as "regex" (which I didn't notice when I provided my answer) so what I think is irrelevant :-) – ChssPly76 Oct 19 '09 at 21:57
  • 1
    @ChssPly76: If the OP wrongly presupposes using a RegEx to solve the problem and you point out the pitfalls, then what you think is completely relevant. – Lawrence Dol Oct 20 '09 at 01:37