You need a piece of code that allows you to replace special characters with numbers that will go in order.

Example:

Enter:

Текст#abc Текст#abc Текст#abc 

Output:

 Текст#1 Текст#2 Текст#3 

I remember before I did such a thing, only now I forgot, but I need it .. Please help)

  • Regular expressions? Find a match and change each group in the loop - Serhii Dikobrazko

2 answers 2

For example, so ( all code ):

 String text = "Текст#abc\nТекст#abc\nТекст#abc"; String toReplace = "abc"; int number = 1; while (text.contains(toReplace)) { text = text.replaceFirst(toReplace, "" + number); number++; } System.out.println(text); 

Console:

 Текст#1 Текст#2 Текст#3 

    The answer may differ slightly depending on where you have the text, in one place or in different ones, but the principle will be the same: create a variable, which will be our inserted number, increasing when we find a place to replace, break the text into lines, change and merge back. It should end up with something like this:

     int num=0; String toReplace="abc"; String[] strings=text.split(" "); String result=""; for(String string:strings){ num++; if(result.euals("")){ result=string.replace(toReplace,num+""); }else{ result+=string.replace(toReplace,num+""); } } text=replace;