Is it possible in Java to find the N number (user input) entry under the lines, and only replace it if it is, and not change all the text.

Suppose you entered the string pppooopppooocccpppbb , look for the value of ооо inside and change it to ddd , but for now the program changes all the text to ooo

 public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Введите текст: "); String s = scan.nextLine(); System.out.println("Введенный текст: "+s); System.out.print("Введите искомую строку: "); String str1 = scan.nextLine(); System.out.print("Введите заменяющую строку: "); String str2 = scan.nextLine(); s=s.replaceAll(str1, str2); System.out.println(s); } 
  • the question is incomprehensible. do you program changes the entry, what is wrong then? what do you want ? - Senior Pomidor
  • one
    @Nosferaty, a why do you use replaceAll (All, dare) if you need to change one entry? - ArchDemon
  • @ArchDemon, the problem is that just with replace, it works the same way - Nosferaty
  • @Nosferaty will have to write his method then - Senior Pomidor
  • This code, according to your example, displays pppdddpppdddcccpppbb for me, was it expected? Add the result you want to see. - MrFylypenko

1 answer 1

Change reaplceAll to replaceFirst .