0

I want to insert chars at specific location alternatively like :

input : orange

output : or##an##ge##

This is what I've tried:

String output = input.substring(x,x+2) + "00" + input.substring(x+2,x+4) + "00" + input.substring(x+4,x+6) + "00" + input.substring(x+6,7) + "00";
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
HelloWorld
  • 382
  • 1
  • 13
  • Have you tried coding it? – ItamarG3 Dec 01 '16 at 11:21
  • Bonus points to anyone who can do this in a single line with `String.replaceAll`. – Tim Biegeleisen Dec 01 '16 at 11:23
  • @ItamarGreen Yeah I tried using substring ..also got my output..I need to have the same format for any input .... input : programming output : pr##og##ra##mm##in##g## – HelloWorld Dec 01 '16 at 11:27
  • @Avudai Well... always post what you've tried... – ItamarG3 Dec 01 '16 at 11:28
  • String output = input.substring(x,x+2) + "00" + input.substring(x+2,x+4) + "00" + input.substring(x+4,x+6) + "00" + input.substring(x+6,7) + "00" ; @ItamarGreen – HelloWorld Dec 01 '16 at 11:31
  • @Avudai in the question body – ItamarG3 Dec 01 '16 at 11:41
  • @Tim `System.out.println("orange".replaceAll(".{2}","$0pp"));` Is there a better one? – Curious Dec 01 '16 at 11:53
  • 1
    @jay Bummer you should have posted that earlier. – Tim Biegeleisen Dec 01 '16 at 11:54
  • Hey guys ,,,I am getting my desired output only if my string length is 7,else it throws "String index out of range" .... Am stuck at the end of code {input.substring(x+6,7)} <<< String output = input.substring(x,x+2) + "00" + input.substring(x+2,x+4) + "00" + input.substring(x+4,x+6) + "00" + input.substring(x+6,7) + "00" >>> @jay – HelloWorld Dec 01 '16 at 12:01

0 Answers0