Le Remplacement de la classe Java String() La méthode renvoie une chaîne remplaçant tous les anciens char ou CharSequence par un nouveau char ou CharSequence.
Depuis JDK 1.5, une nouvelle méthode replace() est introduite qui nous permet de remplacer une séquence de valeurs char.
entier une chaîne java
Signature
Il existe deux types de méthodes replace() dans la classe Java String.
public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement)
La deuxième méthode replace() est ajoutée depuis JDK 1.5.
Paramètres
vieuxChar : ancien personnage
nouveauChar : Nouveau personnage
cible : séquence cible de caractères
remplacement : séquence de caractères de remplacement
Retour
chaîne remplacée
Lancements d'exceptions
NullPointerException : si le remplacement ou la cible est égal à null.
Implémentation interne
public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i <len) { if (val[i]="=" oldchar) break; } (i < len) char buf[]="new" char[len]; for (int j="0;" i; j++) buf[j]="val[j];" while c="val[i];" buf[i]="(c" =="oldChar)" ? newchar : c; i++; return new string(buf, true); this; pre> <pre> public String replace(CharSequence target, CharSequence replacement) { return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( this).replaceAll(Matcher.quoteReplacement(replacement.toString())); } </pre> <h2>Java String replace(char old, char new) method example</h2> <p> <strong>FileName:</strong> ReplaceExample1.java</p> <pre> public class ReplaceExample1{ public static void main(String args[]){ String s1='javatpoint is a very good website'; String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e' System.out.println(replaceString); }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> jevetpoint is e very good website </pre> <h2>Java String replace(CharSequence target, CharSequence replacement) method example</h2> <p> <strong>FileName:</strong> ReplaceExample2.java</p> <pre> public class ReplaceExample2{ public static void main(String args[]){ String s1='my name is khan my name is java'; String replaceString=s1.replace('is','was');//replaces all occurrences of 'is' to 'was' System.out.println(replaceString); }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> my name was khan my name was java </pre> <h2>Java String replace() Method Example 3</h2> <p> <strong>FileName:</strong> ReplaceExample3.java</p> <pre> public class ReplaceExample3 { public static void main(String[] args) { String str = 'oooooo-hhhh-oooooo'; String rs = str.replace('h','s'); // Replace 'h' with 's' System.out.println(rs); rs = rs.replace('s','h'); // Replace 's' with 'h' System.out.println(rs); } }</pre> <p> <strong>Output:</strong> </p> <pre>oooooo-ssss-oooooo oooooo-hhhh-oooooo </pre> <h2>Java String replace() Method Example 4</h2> <p>The replace() method throws the NullPointerException when the replacement or target is null. The following example confirms the same.</p> <p> <strong>FileName:</strong> ReplaceExample4.java</p> <pre> public class ReplaceExample4 { // main method public static void main(String argvs[]) { String str = 'For learning Java, JavaTpoint is a very good site.'; int size = str.length(); System.out.println(str); String target = null; // replacing null with JavaTpoint. Hence, the NullPointerException is raised. str = str.replace(target, 'JavaTpoint '); System.out.println(str); } } </pre> <p> <strong>Output:</strong> </p> <pre> For learning Java, JavaTpoint is a very good site. Exception in thread 'main' java.lang.NullPointerException at java.base/java.lang.String.replace(String.java:2142) at ReplaceExample4.main(ReplaceExample4.java:12) </pre> <hr></len)>
Exemple de méthode Java String replace (char old, char new)
Nom de fichier: RemplacerExample1.java
pyspark sql
public class ReplaceExample1{ public static void main(String args[]){ String s1='javatpoint is a very good website'; String replaceString=s1.replace('a','e');//replaces all occurrences of 'a' to 'e' System.out.println(replaceString); }}Testez-le maintenant
Sortir:
jevetpoint is e very good website
Exemple de méthode Java String replace (cible CharSequence, remplacement CharSequence)
Nom de fichier: RemplacerExample2.java
public class ReplaceExample2{ public static void main(String args[]){ String s1='my name is khan my name is java'; String replaceString=s1.replace('is','was');//replaces all occurrences of 'is' to 'was' System.out.println(replaceString); }}Testez-le maintenant
Sortir:
my name was khan my name was java
Méthode Java String replace() Exemple 3
Nom de fichier: RemplacerExample3.java
public class ReplaceExample3 { public static void main(String[] args) { String str = 'oooooo-hhhh-oooooo'; String rs = str.replace('h','s'); // Replace 'h' with 's' System.out.println(rs); rs = rs.replace('s','h'); // Replace 's' with 'h' System.out.println(rs); } }
Sortir:
objet en programmation Java
oooooo-ssss-oooooo oooooo-hhhh-oooooo
Méthode Java String replace() Exemple 4
La méthode replace() renvoie l'exception NullPointerException lorsque le remplacement ou la cible est nulle. L’exemple suivant confirme la même chose.
Nom de fichier: RemplacerExample4.java
public class ReplaceExample4 { // main method public static void main(String argvs[]) { String str = 'For learning Java, JavaTpoint is a very good site.'; int size = str.length(); System.out.println(str); String target = null; // replacing null with JavaTpoint. Hence, the NullPointerException is raised. str = str.replace(target, 'JavaTpoint '); System.out.println(str); } }
Sortir:
For learning Java, JavaTpoint is a very good site. Exception in thread 'main' java.lang.NullPointerException at java.base/java.lang.String.replace(String.java:2142) at ReplaceExample4.main(ReplaceExample4.java:12)