logo

Јава стринг реплаце()

Тхе Јава Стринг цласс реплаце() метода враћа стринг који замењује све старе цхар или ЦхарСекуенце новим цхар или ЦхарСекуенце.

Од ЈДК 1.5 уведена је нова метода реплаце() која нам омогућава да заменимо низ вредности цхар.

спајање низова

Потпис

Постоје две врсте метода реплаце() у класи Јава Стринг.

 public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) 

Други метод реплаце() је додат од ЈДК 1.5.

Параметерс

олдЦхар : стари лик

невЦхар : нови карактер

циљ : циљни низ знакова

замена : замена низа знакова

Повратак

замењен низ

шта је андроид ускршње јаје

Изузетак баца

НуллПоинтерЕкцептион: ако је замена или циљ једнак нулл.

Интерна имплементација

 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=&apos;javatpoint is a very good website&apos;; String replaceString=s1.replace(&apos;a&apos;,&apos;e&apos;);//replaces all occurrences of &apos;a&apos; to &apos;e&apos; 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=&apos;my name is khan my name is java&apos;; String replaceString=s1.replace(&apos;is&apos;,&apos;was&apos;);//replaces all occurrences of &apos;is&apos; to &apos;was&apos; 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 = &apos;oooooo-hhhh-oooooo&apos;; String rs = str.replace(&apos;h&apos;,&apos;s&apos;); // Replace &apos;h&apos; with &apos;s&apos; System.out.println(rs); rs = rs.replace(&apos;s&apos;,&apos;h&apos;); // Replace &apos;s&apos; with &apos;h&apos; 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 = &apos;For learning Java, JavaTpoint is a very good site.&apos;; int size = str.length(); System.out.println(str); String target = null; // replacing null with JavaTpoint. Hence, the NullPointerException is raised. str = str.replace(target, &apos;JavaTpoint &apos;); System.out.println(str); } } </pre> <p> <strong>Output:</strong> </p> <pre> For learning Java, JavaTpoint is a very good site. Exception in thread &apos;main&apos; java.lang.NullPointerException at java.base/java.lang.String.replace(String.java:2142) at ReplaceExample4.main(ReplaceExample4.java:12) </pre> <hr></len)>

Пример методе замене Јава стринга (старо цхар, ново).

Назив документа: РеплацеЕкампле1.јава

 public class ReplaceExample1{ public static void main(String args[]){ String s1=&apos;javatpoint is a very good website&apos;; String replaceString=s1.replace(&apos;a&apos;,&apos;e&apos;);//replaces all occurrences of &apos;a&apos; to &apos;e&apos; System.out.println(replaceString); }} 
Тестирајте одмах

Излаз:

 jevetpoint is e very good website 

Пример метода замене Јава стринга (циљ ЦхарСекуенце, замена ЦхарСекуенце).

Назив документа: РеплацеЕкампле2.јава

 public class ReplaceExample2{ public static void main(String args[]){ String s1=&apos;my name is khan my name is java&apos;; String replaceString=s1.replace(&apos;is&apos;,&apos;was&apos;);//replaces all occurrences of &apos;is&apos; to &apos;was&apos; System.out.println(replaceString); }} 
Тестирајте одмах

Излаз:

 my name was khan my name was java 

Јава стринг реплаце() Пример методе 3

Назив документа: РеплацеЕкампле3.јава

 public class ReplaceExample3 { public static void main(String[] args) { String str = &apos;oooooo-hhhh-oooooo&apos;; String rs = str.replace(&apos;h&apos;,&apos;s&apos;); // Replace &apos;h&apos; with &apos;s&apos; System.out.println(rs); rs = rs.replace(&apos;s&apos;,&apos;h&apos;); // Replace &apos;s&apos; with &apos;h&apos; System.out.println(rs); } }

Излаз:

замените
oooooo-ssss-oooooo oooooo-hhhh-oooooo 

Јава стринг реплаце() Пример методе 4

Метод реплаце() избацује НуллПоинтерЕкцептион када је замена или циљ нулл. Следећи пример потврђује исто.

Назив документа: РеплацеЕкампле4.јава

 public class ReplaceExample4 { // main method public static void main(String argvs[]) { String str = &apos;For learning Java, JavaTpoint is a very good site.&apos;; int size = str.length(); System.out.println(str); String target = null; // replacing null with JavaTpoint. Hence, the NullPointerException is raised. str = str.replace(target, &apos;JavaTpoint &apos;); System.out.println(str); } } 

Излаз:

 For learning Java, JavaTpoint is a very good site. Exception in thread &apos;main&apos; java.lang.NullPointerException at java.base/java.lang.String.replace(String.java:2142) at ReplaceExample4.main(ReplaceExample4.java:12)