Home > Java > javaTutorial > Java Example - String Replacement

Java Example - String Replacement

黄舟
Release: 2017-02-21 10:06:36
Original
1603 people have browsed it

How to replace characters in a string using java?

In the following example, we use the replace method of the java String class to replace characters in the string:

public class StringReplaceEmp{
   public static void main(String args[]){
      String str="Hello World";
      System.out.println( str.replace( 'H','W' ) );
      System.out.println( str.replaceFirst("He", "Wa") );
      System.out.println( str.replaceAll("He", "Ha") );
   }}
Copy after login

The output result of the above code example is:

Wello World
Wallo World
Hallo World
Copy after login

The above is the Java example - Contents of string replacement, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template