Java replace () character replacement function

Source: Internet
Author: User

Public String replace (char oldChar, char newChar)
Returns a new string, which is obtained by replacing all the oldChar in the string with newChar.
If oldChar does not appear in the character sequence represented by this String object, a reference to this String object is returned.
Otherwise, create a new String object, which represents the character sequence except that all oldChar is replaced with newChar, which is the same as the character sequence represented by this String object.

Public class MainClass
{
Public static void main (String args [])
   {
String s1 = new String ("hello ");
String s2 = new String ("GOODBYE ");
String s3 = new String ("spaces ");

System. out. printf ("s1 = % sns2 = % sns3 = % snn", s1, s2, s3 );

// Test method replace
System. out. printf ("Replace 'L' with 'l' in s1: % snn", s1.replace ('L', 'L '));


} // End main
}

For special nr, we replace it with br.

Public class Main {
Public static void main (String [] argv ){

System. out. println ("rn | r | n | nr". replaceAll ("(rn | r | n | nr)", "<br> "));

  }
}
// <Br> | <br>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.