0

I need to replace:

myVariable = "sample string is long " +
  "so I put rest of it in 2nd line." +
  " And sometimes in 3rd and so on";

with:

myVariable = "sample string is long so I put rest of it in 2nd line. And sometimes in 3rd and so on";

Additional issue: how to merge entities like above I they have other variables in concatenation chain?

myVar = "The number of the beast is " + numberOfTheBeast + " !!! So I said";

What I'd like to do is to change it into single string with params inside.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Syjmick
  • 51
  • 1
  • 5
  • Dont understand your question. – Achintya Jha Apr 22 '13 at 14:13
  • http://stackoverflow.com/questions/47045/sprintf-equivalent-in-java – torquestomp Apr 22 '13 at 14:13
  • Please don't post multiple issues in one question. Your first issue is an IDE-specific question - which IDE are you using? Also... why are you doing this? – Duncan Jones Apr 22 '13 at 14:18
  • You mean in code or in runtime? I don't really understand the question itself. – CsBalazsHungary Apr 22 '13 at 14:23
  • Let me try to explain why I need to do this. I have this code to maintain and I need to get rid of those string concatenations (also to i18n those strings). – Syjmick Apr 22 '13 at 14:31
  • Those are completely different tasks. The first one is easy to do with a regular expression, the second will probably require a rather huge amount of work, because you need to do type-checking, etc. Writing an IDE extension is probably the easiest way to go (since it has type-checking built-in). – Bernhard Barker Apr 22 '13 at 14:31
  • Dukeling - thanks, I thnik I'll play with IntelliJ – Syjmick Apr 22 '13 at 14:34

1 Answers1

1

I'm not a regular expression guru, but these three regex replaces should do the trick. Most IDEs have a regex replace function.

Replace:     With:
\s*\+\s*\n    +   (space, plus, space)
"\s*\+\s*"   nothing
\s*\+\s*      +   (space, plus, space)

You might have to use \s*+\s*\r\n for the first regex, if you are on Windows.

Crunch
  • 500
  • 3
  • 9