-2

For my case, I want replace all my code that contain character ; to ;\n but, I don't want to do it for characters inside for loop.

int a=5;String B="";
for(int i=0;i<a;i++)
    System.out.println(i);

so, I want result like this :

int a=5;
String B="";
for(int i=0;i<a;i++)
    System.out.println(i);

I have no idea how to make it.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
newbie
  • 929
  • 17
  • 35

3 Answers3

1

I would suggest to do it something like this:- When you find the index of for then next find index of right parenthesis ")"and then jump that much indexes.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
0
B.replaceAll(";", ";\n")

this will replace all instances of ; with ;\n

AbtPst
  • 7,778
  • 17
  • 91
  • 172
  • how to inside for loop? – newbie Dec 31 '15 at 11:29
  • for loop would be a bad idea as we are replacing 1 character with 2. replaceAll takes care of that internally. if we have to do it then everytime we find the ; we have to shift all the remaining characters by 1 place to make room for the \n – AbtPst Dec 31 '15 at 11:34
  • so what should i do for my case? do you have best way beside regex. – newbie Dec 31 '15 at 12:57
  • there is a way to do this using lists. can you provide some sample input? – AbtPst Dec 31 '15 at 15:08
0

No need to use regex for this task. Any IDE(example Eclipse) and even some text editors can do this task for you. Use regex for manipulating text not your program code. Click here to see how this would be done in eclipse.

Community
  • 1
  • 1
Anonymous
  • 487
  • 1
  • 6
  • 17