The use of StringBuffer

Source: Internet
Author: User

/* Thread Safety (multithreading) * Security-sync-data is secure * unsafe-out of sync-more efficient * security and efficiency issues are problems that will haunt us forever. * Security: Hospital website, Bank website * Efficiency: news website, Forum and Other * * StringBuffer: * Thread-safe variable string. * * The difference between StringBuffer and string? * The former is variable in length and content and the latter is immutable. * If you use the former to do string concatenation, will not waste too much resources. * * StringBuffer Construction Method: * Public StringBuffer (): Non-parametric construction method *public StringBuffer (int capacity): Specifies the capacity of the string buffer object *public stringbuf Fer (String str): Specifies the string content of the string buffer object * * StringBuffer method: *public int Capacity (): Returns the current capacity. Theoretical value *public int length (): Returns the length (number of characters). Actual value */public class Stringbufferdemo {public static void main (string[] args) {//Public stringbuffer (): Non-parametric construction method StringBuffer s b = new StringBuffer (); System.out.println ("SB:" + SB); "" System.out.println ("sb.capacity ():" + sb.capacity ());  16system.out.println ("Sb.length ():" + sb.length ()); 0system.out.println ("--------------------------");//public stringbuffer (int capacity): A string buffer object that specifies the capacity stringbuffer SB2 = new StringBuffer (50); System.out.println ("SB2:" + SB2); "" System.out.println ("sb2.capacity ():" + sb2.capacity ()); 50system.oUt.println ("Sb2.length ():" + sb2.length ()); 0system.out.println ("--------------------------");//Public StringBuffer (String str): A string buffer object that specifies the contents of the string StringBuffer SB3 = new StringBuffer ("Hello"); System.out.println ("SB3:" + SB3); HelloSystem.out.println ("Sb3.capacity ():" + sb3.capacity ()); 21system.out.println ("Sb3.length ():" + sb3.length ()); 5}}
/* * stringbuffer Add functionality: * Public stringbuffer Append (string str): Any type of data can be added to the string buffer, and returns the string buffer itself * public stringbuffer Insert (int offset,string str): Inserts any type of data into the string buffer at the specified location and returns the string buffer itself */public class St Ringbufferdemo {public static void main (string[] args) {//Create a string buffer object StringBuffer SB = new StringBuffer ();//public string Buffer Append (String str)//StringBuffer SB2 = sb.append ("Hello");//System.out.println ("SB:" + SB);// System.out.println ("SB2:" + SB2);//System.out.println (SB = = SB2); true//Add Data//Sb.append ("Hello") in step-by-step,//Sb.append (TRUE);//Sb.append (34.56);//Sb.append (+//), chained programming sb.append (" Hello "). Append (True). Append. Append (34.56); System.out.println ("SB:" + SB);//public stringbuffer Insert (int offset,string//str): Inserts any type of data into the string buffer at the specified location, and returns the string buffer itself Sb.insert (5, "World"); System.out.println ("SB:" + SB);}} 
/* * stringbuffer Delete function * public stringbuffer deletecharat (int index): Deletes the character at the specified position and returns itself * Public StringBuffer Delete (int start,int end): Deletes the end of the specified position starting at the specified position and returns itself */public class Stringbufferdemo {public static void Main (string[] args) {//create object StringBuffer SB = new StringBuffer ();//Add function sb.append ("Hello"). Append ("World"). Append ( "Java"); System.out.println ("SB:" + SB);//Public StringBuffer deletecharat (int index): Deletes the character at the specified position and returns itself//demand: I want to delete e this character, swollen?// Sb.deletecharat (1);//demand: I want to delete the first l this character, swollen?//Sb.deletecharat (1);//public StringBuffer Delete (int start,int//end): Deletes the content at the end of the specified position starting at the specified location, and returns itself//requirements: I want to delete the world this string, swollen?//Sb.delete (5, 10);//demand: I want to delete all data sb.delete (0, Sb.length ()); System.out.println ("SB:" + SB);}} 
/* * StringBuffer Replacement function: * Public stringbuffer replace (int start,int end,string str): Replace */public class stri with str from start to end Ngbufferdemo {public static void main (string[] args) {//Create a string buffer object StringBuffer SB = new StringBuffer ();//Add Data Sb.append (" Hello "); Sb.append (" World "); Sb.append (" Java "); System.out.println ("SB:" + SB);//public StringBuffer replace (int start,int end,string//str): From start to end with STR replacement// Requirements: I want to replace the world data with "Happy Holidays" sb.replace (5, 10, "Happy Holidays"); System.out.println ("SB:" + SB);}}
/* * StringBuffer reversal function: * Public stringbuffer reverse () */public class Stringbufferdemo {public static void main (string[] args) {//Create a string buffer object StringBuffer SB = new StringBuffer ();//Add Data sb.append ("Xia Qing lin Love Me"); System.out.println ("SB:" + SB);//public StringBuffer reverse () sb.reverse (); System.out.println ("SB:" + SB);}}
/* * stringbuffer interception function: Note The return value type is no longer stringbuffer itself * public String substring (int start) * Public String substring (int start,int end) */public class Stringbufferdemo {public static void main (string[] args) {//Create Word The string buffer object StringBuffer SB = new StringBuffer ();//add Element Sb.append ("Hello"). Append ("World"). Append ("Java"); System.out.println ("SB:" + SB);//intercept function//public string substring (int start) string s = sb.substring (5); System.out.println ("s:" + s); System.out.println ("SB:" + SB);//public string substring (int start,int end) String ss = Sb.substring (5, 10); SYSTEM.OUT.PRINTLN ("SS:" + ss); System.out.println ("SB:" + SB);}} 
/* * Why do we explain the conversion between classes: * A--B conversion * We convert A to B, actually to use the function of B. * B--a conversion * We may want the result is a type, so we have to turn back. * * String and StringBuffer are converted to each other? */public class Stringbuffertest {public static void main (string[] args) {//String--stringbufferstring s = "Hello";//Note : The value of the string cannot be assigned directly to stringbuffer//stringbuffer sb = "Hello";//StringBuffer SB = s;//Mode 1: By construction method StringBuffer SB = new Stringbuf Fer (s);//Mode 2: Through Append () method StringBuffer SB2 = new StringBuffer (); Sb2.append (s); System.out.println ("SB:" + SB); System.out.println ("SB2:" + SB2); System.out.println ("---------------");//StringBuffer--Stringstringbuffer buffer = new StringBuffer ("Java");// String (stringbuffer buffer)//mode 1: By constructing the method string str = new string (buffer);//Mode 2: Through the ToString () method String str2 = Buffer.tostring (); System.out.println ("str:" + str); System.out.println ("str2:" + str2);}} 
/* * stitching an array into a string */public class StringBufferTest2 {public static void main (string[] args) { Define an array int[] arr = {44, 33, 55, 11, 22};//define the function//mode 1: String to stitch the way string S1 = arraytostring (arr); System.out.println ("S1:" + S1);//Mode 2: The way to do stitching with stringbuffer string s2 = arrayToString2 (arr); System.out.println ("s2:" + s2);} Use StringBuffer to make stitching public static String arrayToString2 (int[] arr) {StringBuffer sb = new StringBuffer (); Sb.append ("["  ), for (int x = 0, x < Arr.length, + +) {if (x = = arr.length-1) {sb.append (arr[x]),} else {sb.append (arr[x]). Append (", ");}} Sb.append ("]"); return sb.tostring ();} Use String to stitch the public static string arraytostring (int[] arr) {string s = ""; s + = "["; for (int x = 0; x < arr.length; X + +) {if (× = = Arr.length-1) {s + = arr[x];} else {s + = Arr[x];s + = ",";}} s + = "]"; return s;}} 
import java.util.scanner;/* * Reverse string */public class StringBufferTest3 {public static void Main (string[] args) {//keyboard input data Scanner sc = new Scanner (system.in); System.out.println ("Please enter data:"); string s = Sc.nextline ();//mode 1: string concatenation string S1 = Myreverse (s); System.out.println ("S1:" + S1);//Mode 2: Reverse () function with stringbuffer string s2 = MyReverse2 (s); System.out.println ("s2:" + s2);} Use StringBuffer's reverse () function public static String MyReverse2 (String s) {//StringBuffer SB = new StringBuffer ();//Sb.appen D (s);//StringBuffer SB = new StringBuffer (s);//Sb.reverse ();//return sb.tostring ();//Simple version return new StringBuffer (s). Re Verse (). toString ();} Use String to stitch public static string Myreverse (string s) {string result = ""; char[] CHS = S.tochararray (); for (int x = Chs.le Ngth-1; x >= 0; x--) {//char ch = chs[x];//result + = Ch;result + chs[x];} return result;}} 
Import java.util.scanner;/* * Determine if a string is a symmetric string * For example "ABC" is not a symmetric string, "ABA", "ABBA", "AAA", "MNANM" is a symmetric string * * Analysis: * Determine if a string is a symmetric word String, I just need to compare * First and last comparison * Second and penultimate comparison * ... * the number of comparisons is the length divided by 2. */public class StringBufferTest4 {public static void main (string[] args) {//Create keyboard Entry Object Scanner sc = new Scanner (system.in); S Ystem.out.println ("Please enter a string:"); String s = sc.nextline ();//One of the comparison Boolean B = Issame (s); System.out.println ("B:" + b);//inverse function with string buffers Boolean b2 = IsSame2 (s); System.out.println ("B2:" +B2);} public static Boolean isSame2 (String s) {return new StringBuffer (s). Reverse (). ToString (). Equals (s); public static Boolean Issame (string s) {/////convert string to character array//char[] CHS = S.tochararray ();////for (int start = 0, end = Chs.length-1; Start <= end; start++, end--) {//if (Chs[start]! = Chs[end]) {//return false;//}//}////return true;//}public static Boolean Issame (String s) {Boolean flag = true;//The string to a character array char[] CHS = S.tochararray (); for (int start = 0, end = chs.length-1; start <= end; Start+ +, end--) {if (Chs[start]! = Chs[end]) {flag = False;break;}} return flag;}}
/* * Interview Question: * 1:string,stringbuffer,stringbuilder difference? * A:string content is immutable, and stringbuffer,stringbuilder is content-changeable. * B:stringbuffer is synchronous, data security, low efficiency; StringBuilder is unsynchronized, data insecure, high efficiency * * 2:stringbuffer and array differences? * Both can be seen as a container, loaded with other data. * But, StringBuffer's data is ultimately a string of data. * While arrays can place a variety of data, they must be of the same data type. * * 3: Formal Parameter Problem * string passed as parameter * StringBuffer as parameter * * Formal parameter: * Basic type: Change of formal parameter does not affect actual parameter * Reference type: Change of formal parameter directly affects actual parameter * * NOTE: * stri Ng is passed as a parameter, and the effect and the base type are passed as arguments. */public class Stringbufferdemo {public static void main (string[] args) {String S1 = "Hello"; String s2 = "World"; SYSTEM.OUT.PRINTLN (S1 + "---" + s2);//Hello---worldchange (s1, S2); SYSTEM.OUT.PRINTLN (S1 + "---" + s2);//Hello---worldstringbuffer sb1 = new StringBuffer ("Hello"); StringBuffer SB2 = new StringBuffer ("World"); System.out.println (SB1 + "---" + sb2);//Hello---worldchange (sb1, SB2);  System.out.println (SB1 + "---" + sb2);//Hello---worldworld}public static void Change (StringBuffer sb1, StringBuffer SB2) {SB1 = Sb2;sb2.append (SB1);} public static void Change (StrinG S1, String s2) {S1 = S2;s2 = s1 + s2;}} 

The use of StringBuffer

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.