How to use the replace () method (Java Silver)

replace method

** Replace string **

The replace method searches the target character string for the character string specified by the first argument, and replaces the matched character string with the character string specified by the second argument. Then, the replaced character string is returned as a return value. String replacement starts at the beginning and continues to the end of the string.

Simply put, ** replace the character string specified by the first argument with the character string specified by the second argument **.

String class methods java.lang.String.replace()

How to use

Target string.replace(String to be replaced,Replacement string)

Example

	public static void main(String[] args) {

		  //Target string
		  String strBefore = "Hello world!";

		  //String to be replaced"Hello"Replace string"Goodbye"Replace with
		  String strAfter = strBefore.replace("Hello", "Goodbye");

		  System.out.println(strAfter);
		}

Execution result

Goodbye world!

When there are multiple places to replace

If there are multiple parts to be replaced, all the corresponding parts are replaced.

	public static void main(String[] args) {

		  // //Target string
		  String strBefore = "aabbaacc aab ";

		  // "aa"→"DDD"Replace with
		  String strAfter = strBefore.replace("aa", "DDD");

		  System.out.println(strAfter);

		}

Execution result

DDDbbDDDcc DDDb 

String replacement starts in order from the beginning

In the example below, the first AAA and the next AAA are replaced with B. Therefore, it does not become AABB or ABBA.

	public static void main(String[] args) {

		  //8 strings A before replacement
		  String strBefore = "AAAAAAAA";

		  // "AAA"→"B"Replace with
		  String strAfter = strBefore.replace("AAA", "B");

		  System.out.println(strAfter);

		}

Execution result


BBAA

About arguments

The replace method is overloaded, and some receive two ** char ** arguments and some receive two ** CharSequence ** arguments. Note that there is no overload that accepts mixed arguments of char and CharSequence types.

Recommended Posts

How to use the replace () method (Java Silver)
How to use submit method (Java Silver)
[Java] How to use the toString () method
How to use the link_to method
How to use the include? method
How to use the form_with method
[Java] How to use join method
[Java] How to use the File class
[Java] How to use the hasNext function
[Java] How to use the HashMap class
[Rails] How to use the map method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use the Calendar class
Output of how to use the slice method
[Ruby basics] How to use the slice method
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java variables
[Java] How to use Optional ①
[Java] How to use compareTo method of Date class
How to study Java Silver SE 8
How to use Java HttpClient (Get)
How to use the wrapper class
[Processing × Java] How to use variables
[Ruby] How to use any? Method
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to pass Oracle Java Silver
[Processing × Java] How to use arrays
How to use Ruby inject method
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
How to get the class name / method name running in Java
Multilingual Locale in Java How to use Locale
[Java] How to compare with equals method
Java SE8 Silver ~ The Road to Pass ~
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to set the Date time to 00:00:00
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
How to install the legacy version [Java]
How to use Java Scanner class (Note)
How to get the date in java
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Rails] How to use helper method, confimartion
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation