1, the replacement function of StringBuffer:
Public stringbuffer replace (int start,int end,string str): Replace with str from start to end
1 Public classStringbufferdemo {2 Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //Add Data7Sb.append ("Hello");8Sb.append ("World");9Sb.append ("Java");TenSystem.out.println ("SB:" +SB); One A //Public stringbuffer replace (int start,int end,string - //str): Replace with str from start to end - //need: I want to replace the world data with "Happy Holidays" theSb.replace (5, 10, "Happy Holidays"); -System.out.println ("SB:" +SB); - } -}
2, StringBuffer reversal function:
Public StringBuffer Reverse ()
1 Public classStringbufferdemo {2 Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //Add Data7Sb.append ("Xia Qing lin Love Me");8System.out.println ("SB:" +SB);9 Ten //Public StringBuffer Reverse () One sb.reverse (); ASystem.out.println ("SB:" +SB); - } -}
3, StringBuffer interception function: Note The return value type is no longer the stringbuffer itself, but the string
Public String substring (int start)
Public String substring (int start,int end)
1 Public classStringbufferdemo {2 Public Static voidMain (string[] args) {3 //creating a String buffer object4StringBuffer SB =NewStringBuffer ();5 6 //adding elements7Sb.append ("Hello"). Append ("World"). Append ("Java");8System.out.println ("SB:" +SB);9 Ten //interception function One //Public String substring (int start) AString s = sb.substring (5); -System.out.println ("s:" + s);//Worldjava -System.out.println ("SB:" + SB);//Helloworldjava, because the above returns a string, not itself, so it does not change the - //Public String substring (int start,int end) -String SS = sb.substring (5, 10); -SYSTEM.OUT.PRINTLN ("SS:" + ss);// World +System.out.println ("SB:" + SB);//Helloworldjava - } +}
Java 12-4 StringBuffer class substitution, inversion, interception function