Differenza tra replace() e replaceAll() in Java

Differenza tra replace() e replaceAll() in Java

La classe Java String fornisce vari metodi per manipolare la stringa. IL sostituire() E sostituisci tutto() Il metodo è uno di quelli utilizzati per sostituire una stringa con una sottostringa specificata. Poiché il nome di entrambi i metodi suona uguale, ma il loro funzionamento è diverso.

Comprendiamo i metodi replace(), replaceAll() e replaceFirst() e differenziamo ciascuno di essi uno per uno.

Metodo String.replace()

IL sostituire() Il metodo è uno dei metodi di stringa più utilizzati per sostituire tutte le occorrenze di un carattere con il carattere specificato. Il metodo replace() di JDK 1.5 sostituisce il char e una sequenza di valori char.

Sintassi:

Questi sono i seguenti due tipi di sostituire() metodi nella classe String Java.

 public string replace(char prevChar, char newChar) public String replace(CharSequence target, CharSequence replacement)  

Parametri

carattere precedente: Il parametro definisce il carattere della stringa da sostituire.

nuovoCarattere: Il parametro definisce il newChar che prende il posto del prevChar.

bersaglio: Il parametro definisce la sequenza di caratteri di destinazione.

sostituzione: Il parametro definisce la sequenza di sostituzione dei caratteri.

Facciamo un esempio per capire come possiamo utilizzare il metodo replace() nei programmi Java.

Sostituisci Esempio1.java

 // import required classes and packages package javaTpoint.JavaExample; import java.util.Scanner; // create class ReplaceExample1 to understand both type of replace() method public class ReplaceExample1 { // main() method start public static void main(String args[]){ // declare str1 str2, c, and subStr String str1, str2, targetStr, replacementStr; char prevChar, newChar; // create instance of Scanner class to take input string from user Scanner sc = new Scanner(System.in); // take 1st string from user System.out.println('Enter 1st string :'); str1 = sc.nextLine(); System.out.println('Enter 2nd string :'); str2 = sc.nextLine(); System.out.println('Enter the target string for 1st string :'); targetStr = sc.nextLine(); System.out.println('Enter the replacement string for 1st string :'); replacementStr = sc.nextLine(); System.out.println('Enter character which you want to replace in 2nd string :'); prevChar = sc.next().charAt(0); System.out.println('Enter character which take place of :'+prevChar+' in 2nd string :'); newChar = sc.next().charAt(0); sc.close(); String replaceStr1 = str2.replace(prevChar, newChar);//replaces all occurrences of prevChar to newChar System.out.println('String after replacing '+prevChar+' with '+newChar+' is: '+replaceStr1); String replaceStr2 = str1.replace(targetStr, replacementStr);//replaces all occurrences of targetStr with replacementStr System.out.println('String after replacing '+targetStr+' with '+replacementStr+' is: '+replaceStr2); } }  

Produzione:

replace() vs replaceAll() in Java

Metodo String.replaceAll()

IL sostituisci tutto() Il metodo è simile al metodo String.replaceFirst(). L'unica differenza tra loro è che sostituisce la sottostringa con la stringa data per tutte le occorrenze presenti nella stringa.

Sintassi:

La sintassi del metodo replaceAll() è la seguente:

 public String replaceAll(String str, String replacement)  

Parametri

stringa: Il parametro definisce la sottostringa che dobbiamo sostituire nella stringa.

sostituzione: Il parametro definisce la stringa sostitutiva che prende il posto della str.

Facciamo un esempio per capire come possiamo utilizzare il metodo replaceAll() nei nostri programmi.

Sostituisci Esempio3.java

 // import required classes and packages package javaTpoint.JavaExample; import java.util.Scanner; // create class ReplaceExample3 to understand replaceAll() method public class ReplaceExample3 { // main() method start public static void main(String args[]){ // declare str1 str2, subStr String str1, targetStr, replacementStr; // create instance of Scanner class to take input string from user Scanner sc = new Scanner(System.in); // take 1st string from user System.out.println('Enter 1st string :'); str1 = sc.nextLine(); System.out.println('Enter the target string for 1st string :'); targetStr = sc.nextLine(); System.out.println('Enter the replacement string for 1st string :'); replacementStr = sc.nextLine(); sc.close(); String replaceStr1 = str1.replaceAll(targetStr, replacementStr);//replaces all occurrences of targetStr to replacementStr System.out.println('String after replacing '+targetStr+' with '+replacementStr+' is: '+replaceStr1); } }  

Produzione:

replace() vs replaceAll() in Java

Metodo String.replaceFirst()

IL sostituisciPrimo() Il metodo è un altro metodo per sostituire la sottostringa. Sostituisce la sottostringa con la stringa specificata. Il metodo replaceFirst() sostituisce solo la prima occorrenza della sottostringa.

Sintassi:

La sintassi del metodo replaceFirst() è la seguente:

 public String replaceFirst(String str, String replacement)  

Parametri

Forza: Il parametro definisce la sottostringa che dobbiamo sostituire nella stringa.

Sostituzione: Il parametro definisce la stringa sostitutiva che prende il posto della str.

Facciamo un esempio per capire come possiamo utilizzare il metodo replaceFirst() nei nostri programmi.

Sostituisci Esempio2.java

 // import required classes and packages package javaTpoint.JavaExample; import java.util.Scanner; // create class ReplaceExample2 to understand replaceFirst() method public class ReplaceExample2 { // main() method start public static void main(String args[]){ // declare str1 str2, subStr String str1, targetStr, replacementStr; // create instance of Scanner class to take input string from user Scanner sc = new Scanner(System.in); // take 1st string from user System.out.println('Enter 1st string :'); str1 = sc.nextLine(); System.out.println('Enter the target string for 1st string :'); targetStr = sc.nextLine(); System.out.println('Enter the replacement string for 1st string :'); replacementStr = sc.nextLine(); sc.close(); String replaceStr1 = str1.replaceFirst(targetStr, replacementStr);//replaces first occurrences of targetStr to replacementStr System.out.println('String after replacing '+targetStr+' with '+replacementStr+' is: '+replaceStr1); }  

Produzione:

replace() vs replaceAll() in Java