Operation method of StringBuilder string type in Java and API collation _java

Source: Internet
Author: User
Tags object object stringbuffer

0.StringBuilder Type Introduction
The StringBuilder type is a variable string type, and the StringBuilder type API is basically consistent with the StringBuffer type API, the only difference being that the use of StringBuilder is assumed to be in a single thread, in other words , StringBuilder is not thread safe. StringBuilder, when instantiated, usually sets a capacity size by default, typically +16 of the length of the string parameter. The StringBuilder is inherited from the abstract class, and the abstract class is implemented using a character array, which can be dynamically extended. Abstractstringbuilder The common methods provided by the StringBuilder class are append (), insert (), replace (), Deletecharat (), indexOf (), reverse (), toString (), and so on. Can realize the string of additions and deletions to check and other basic functions.

Package date0812.demo1;

public class Test3 {public

  static void Main (string[] args) {
    StringBuilder StringBuilder = new StringBuilder ("Ecl Ipse ");
    Add
    StringBuilder = stringbuilder.append ("Software");
    capacity
    int C = stringbuilder.capacity ();
    Flip
    stringbuilder= stringbuilder.reverse ();
    System.out.println (C);
    System.out.println (StringBuilder);
  }



Run Result:


Erawtfos Espilce

1. The method of StringBuilder
StringBuilder is a mutable sequence of characters. It inherits from Abstractstringbuilder and implements the Charsequence interface.
StringBuffer is also a subclass of Abstractstringbuilder, but StringBuilder and StringBuffer are not thread-safe, the latter are thread-safe.
The diagram between StringBuilder and Charsequence is as follows:

StringBuilder List of methods:

StringBuilder () StringBuilder (int capacity) StringBuilder (charsequence seq) StringBuilder (String str) StringBuilder AP Pend (float f) StringBuilder append (double D) StringBuilder append (Boolean b) StringBuilder append (int i) stringbuild  ER append (long L) StringBuilder append (char c) StringBuilder append (char[] chars) StringBuilder append (char[) str, int offset, int len) StringBuilder append (String str) StringBuilder append (Object obj) StringBuilder append (stringb Uffer sb) StringBuilder append (charsequence csq) StringBuilder append (charsequence csq, int start, int end) Stringbuil Der Appendcodepoint (int codepoint) int capacity () char charAt (int index) int codepointat (int index) int Codepoin Tbefore (int index) int codepointcount (int start, int end) StringBuilder Delete (int start, int end) StringBuilder del Etecharat (int index) void ensurecapacity (int min) void getChars (int start, int end, char[] DST, int dststart) int in Dexof (String subString,int start) int indexOf (string string) StringBuilder insert (int offset, Boolean b) StringBuilder insert (int offset, I NT i) StringBuilder insert (int offset, long l) StringBuilder insert (int offset, float f) StringBuilder insert (int of Fset, double D StringBuilder Insert (int offset, char c) StringBuilder insert (int offset, char[] ch) StringBuilder I nsert (int offset, char[] str, int stroffset, int strLen) StringBuilder Insert (int offset, String str) StringBuilder in SERT (int offset, Object obj) StringBuilder Insert (int offset, charsequence s) StringBuilder insert (int offset, CHARSEQ uence s, int start, int end) int LastIndexOf (string string) int LastIndexOf (string subString, int start) int length ( int offsetbycodepoints (int index, int codepointoffset) StringBuilder replace (int start, int end, string string) Stri Ngbuilder reverse () void Setcharat (int index, char ch) void SetLength (int length) charsequence subsequence (int sta RT, int end) String substring (int start) string substring (int start, int end) string toString () void TrimToSize ()

 

Because Abstractstringbuilder and StringBuilder source code is too long, here does not list the source code. Interested readers can study for themselves.

2. StringBuilder API test Code
Insert (insert)-related APIs in 2.1 StringBuilder
The source code is as follows (Stringbuilderinserttest.java):

/** * StringBuilder Insert () sample * * @author Skywang/import Java.util.HashMap;
  public class Stringbuilderinserttest {public static void main (string[] args) {Testinsertapis (); /** * StringBuilder Insert () example/private static void Testinsertapis () {System.out.println ("----------

    ----------------------Testinsertapis-------------------------------");

    StringBuilder Sbuilder = new StringBuilder ();
    Inserts a character array at position 0 sbuilder.insert (0, New char[]{' A ', ' B ', ' C ', ' d ', ' e '}); Inserts a character array at position 0.
    0 indicates the starting position of the character array, and 3 represents the length Sbuilder.insert (0, New char[]{' A ', ' B ', ' C ', ' D ', ' E '}, 0, 3);
    Insert float Sbuilder.insert (0, 1.414f) at position 0;
    Insert a double sbuilder.insert (0, 3.14159d) at position 0;
    Inserts a Boolean Sbuilder.insert (0, true) at position 0;
    Insert Char sbuilder.insert (0, ' \ n ') at position 0;
    Insert int Sbuilder.insert (0, 100) at position 0;
    Insert Long Sbuilder.insert (0, 12345L) at position 0; Insert the StringBuilder object Sbuilder.insert at position 0 (0, New StringBuilder ("StriNgbuilder ")); Inserts a StringBuilder object at position 0.
    6 indicates the starting position (including) at position 0 where the object is inserted, 13 is the ending position (excluding) Sbuilder.insert (0, New StringBuilder ("StringBuilder"), 6, 13);
    Inserts a StringBuffer object at position 0.
    Sbuilder.insert (0, New StringBuffer ("StringBuffer")); Inserts a StringBuffer object at position 0.
    6 indicates the starting position (including) at position 0 where the object is inserted, 12 is the ending position (excluding) Sbuilder.insert (0, New StringBuffer ("StringBuffer"), 6, 12);
    Inserts a string object at position 0.
    Sbuilder.insert (0, "String"); Inserts a string object at position 0.
    1 indicates the starting position (including) at position 0 where the object is inserted, and 6 is the ending position (excluding) Sbuilder.insert (0, "0123456789", 1, 6);

    Sbuilder.insert (0, ' \ n '); Inserts an object at position 0.
    Here take HashMap as an example HashMap map = new HashMap ();
    Map.put ("1", "one");
    Map.put ("2", "two");
    Map.put ("3", "three");

    Sbuilder.insert (0, map);
  System.out.printf ("%s\n\n", Sbuilder);

 }
}

Run Result:

--------------------------------Testinsertapis-------------------------------
{3=three, 2=two, 1=one}
12345stringbufferstringbufferbuilderstringbuilder12345100
TRUE3.141591.414ABCABCDE

2.2 StringBuilder Append (append) Related APIs
The source code is as follows (Stringbuilderappendtest.java):

/** * StringBuilder Append () sample * * @author Skywang/import Java.util.HashMap;
  public class Stringbuilderappendtest {public static void main (string[] args) {Testappendapis (); /** * StringBuilder Append () example/private static void Testappendapis () {System.out.println ("----------

    ----------------------Testappendapis-------------------------------");

    StringBuilder Sbuilder = new StringBuilder ();
    Append character array sbuilder.append (new char[]{' A ', ' B ', ' C ', ' d ', ' e '}); Appends an array of characters.
    0 indicates the starting position of the character array, and 3 represents the length Sbuilder.append (new char[]{' A ', ' B ', ' C ', ' D ', ' E '}, 0, 3);
    Add float sbuilder.append (1.414f);
    Double Sbuilder.append (3.14159d) appended;
    Append Boolean sbuilder.append (TRUE);
    Append char sbuilder.append (' \ n ');
    Append int sbuilder.append (100);
    Append long Sbuilder.append (12345L);
    Append StringBuilder Object Sbuilder.append (New StringBuilder ("StringBuilder")); Appends the StringBuilder object.
  6 indicates the starting position of the appended object (including), 13 is the end position (excluding)  Sbuilder.append (New StringBuilder ("StringBuilder"), 6, 13);
    Appends the StringBuffer object.
    Sbuilder.append (New StringBuffer ("StringBuffer")); Appends the StringBuffer object.
    6 indicates the starting position of the appended object (including), 12 is the end position (excluding) Sbuilder.append (new StringBuffer ("StringBuffer"), 6, 12);
    Appends a string object.
    Sbuilder.append ("String"); Appends a string object.
    1 indicates the starting position of the appended object (including), 6 is the end position (excluding) sbuilder.append ("0123456789", 1, 6);

    Sbuilder.append (' \ n '); Append Object object.
    Here take HashMap as an example HashMap map = new HashMap ();
    Map.put ("1", "one");
    Map.put ("2", "two");
    Map.put ("3", "three");
    Sbuilder.append (map);

    Sbuilder.append (' \ n ');  Append Unicode encoding Sbuilder.appendcodepoint (0x5b57);  0x5b57 is the "word" of the Unicode Encoding Sbuilder.appendcodepoint (0X7B26);  0X7B26 is the Unicode encoding Sbuilder.appendcodepoint (0X7F16) of "character";  0x7f16 is a "codec" of the Unicode Encoding Sbuilder.appendcodepoint (0x7801);
  0x7801 is the "code" of the Unicode Encoding System.out.printf ("%s\n\n", Sbuilder);

 }
}

Run Result:

--------------------------------Testappendapis-------------------------------
abcdeabc1.4143.14159true
10012345stringbuilderbuilderstringbufferbufferstring12345
{3=three, 2=two, 1=one}
Character encoding

2.3 StringBuilder in substitution (replace) related APIs
The source code is as follows (Stringbuilderreplacetest.java):

/**
 * StringBuilder replace () sample
 * *
 @author Skywang/
import Java.util.HashMap;

public class Stringbuilderreplacetest {public

  static void Main (string[] args) {
    testreplaceapis ();
  }

  /**
   * StringBuilder replace () example/
  private static void Testreplaceapis () {

    System.out.println (" --------------------------------Testreplaceapis------------------------------");

    StringBuilder Sbuilder;

    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.replace (0, 3, "ABCDE");
    System.out.printf ("sbuilder=%s\n", Sbuilder);

    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.reverse ();
    System.out.printf ("sbuilder=%s\n", Sbuilder);

    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.setcharat (0, ' M ');
    System.out.printf ("sbuilder=%s\n", Sbuilder);

    System.out.println ();
  }


Run Result:

--------------------------------testreplaceapis------------------------------
sbuilder=abcde3456789
sbuilder=9876543210
sbuilder=m123456789

Delete (delete) related APIs in 2.4 StringBuilder
The source code is as follows (Stringbuilderdeletetest.java):

/** * StringBuilder Delete () example * * @author Skywang/import Java.util.HashMap;
  public class Stringbuilderdeletetest {public static void main (string[] args) {Testdeleteapis (); /** * StringBuilder Delete () example/private static void Testdeleteapis () {System.out.println ("----------

    ----------------------Testdeleteapis-------------------------------");

    StringBuilder Sbuilder = new StringBuilder ("0123456789");
    Deletes the character at position 0, and the remaining character is "123456789".
    Sbuilder.deletecharat (0);
    Deletes the character between position 3 (included) and the position 6 (not included), and the remaining character is "123789".

    Sbuilder.delete (3,6);
    Get SB to start with position 1 string str1 = sbuilder.substring (1);
    Get SB from position 3 (included) to position 5 (excluding) string str2 = Sbuilder.substring (3, 5);

    Gets the string from position 3 (included) to position 5 (not included) in SB, and the object is the Charsequence object, which transitions to string string str3 = (string) sbuilder.subsequence (3, 5);
  System.out.printf ("sbuilder=%s\nstr1=%s\nstr2=%s\nstr3=%s\n", Sbuilder, str1, str2, STR3);

 }
}

Run Result:

--------------------------------testdeleteapis-------------------------------
sbuilder=123789
str1= 23789
str2=78
str3=78

Index-related APIs in 2.5 StringBuilder
The source code is as follows (Stringbuilderindextest.java):

/** * StringBuilder Index Related API Demo * * @author Skywang/import Java.util.HashMap;
  public class Stringbuilderindextest {public static void main (string[] args) {Testindexapis (); /** * StringBuilder Index related API demo/private static void Testindexapis () {System.out.println ("----------

    ----------------------Testindexapis--------------------------------");
    StringBuilder Sbuilder = new StringBuilder ("Abcabcabcabcabcabcabcabc");

    System.out.printf ("sbuilder=%s\n", Sbuilder); 1.

    From the point of departure, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.indexof (\" bc\ ")", Sbuilder.indexof ("BC")); 2. Starting from position 5, find out where "BC" first appears System.out.printf ("%-30s =%d\n", "Sbuilder.indexof" ("Bc\", 5) ", Sbuilder.indexof (" BC ", 5)

    ); 3.

    From the back forward, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.lastindexof (\" bc\ ")", Sbuilder.lastindexof ("BC")); 4. Starting from position 4, from the back forward, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.lastindexoF (\ "Bc\", 4) ", Sbuilder.lastindexof (" BC ", 4));
  System.out.println ();

 }
}

Run Result:

--------------------------------Testindexapis--------------------------------
sbuilder= ABCABCABCABCABCABCABCABC
sbuilder.indexof ("BC")     = 1
sbuilder.indexof ("BC", 5)   =
Sbuilder.lastindexof ("BC")   =
sbuilder.lastindexof ("BC", 4) = 4

2.6 StringBuilder The remaining APIs
The source code is as follows (Stringbuilderothertest.java):

/**
 * StringBuilder Other API Sample
 * *
 @author Skywang
/import Java.util.HashMap;

public class Stringbuilderothertest {public

  static void Main (string[] args) {
    testotherapis ();
  }

  /**
   * StringBuilder Other API examples *
   *
  private static void Testotherapis () {

    System.out.println ("------ --------------------------Testotherapis--------------------------------");

    StringBuilder Sbuilder = new StringBuilder ("0123456789");

    int cap = sbuilder.capacity ();
    System.out.printf ("cap=%d\n", cap);

    char C = Sbuilder.charat (6);
    System.out.printf ("c=%c\n", c);

    Char[] Carr = new char[4];
    Sbuilder.getchars (3, 7, Carr, 0);
    for (int i=0; i<carr.length; i++)
      System.out.printf ("carr[%d]=%c", I, Carr[i]);
    System.out.println ();

    System.out.println ();
  }


Run Result:

--------------------------------testotherapis--------------------------------
cap=26
c=6
carr[0]=3 Carr[1]=4 carr[2]=5 carr[3]=6 

3. StringBuilder complete sample
The following example is a complete StringBuilder demo that incorporates the above examples, the source code below (Stringbuildertest.java):

/** * StringBuilder Demo program * * @author Skywang/import Java.util.HashMap;
    public class Stringbuildertest {public static void main (string[] args) {Testotherapis ();
    Testindexapis ();
    Testinsertapis ();
    Testappendapis ();
    Testreplaceapis ();
  Testdeleteapis (); /** * StringBuilder Other API examples * * private static void Testotherapis () {System.out.println ("--------------

    ------------------Testotherapis--------------------------------");

    StringBuilder Sbuilder = new StringBuilder ("0123456789");
    int cap = sbuilder.capacity ();

    System.out.printf ("cap=%d\n", cap);
    char C = Sbuilder.charat (6);

    System.out.printf ("c=%c\n", c);
    Char[] Carr = new CHAR[4];
    Sbuilder.getchars (3, 7, Carr, 0);
    for (int i=0; i<carr.length; i++) System.out.printf ("carr[%d]=%c", I, carr[i]);

    System.out.println ();
  System.out.println (); /** * StringBuilder Index related API demo/private static void Testindexapis () {System.out.println ("--------------------------------testindexapis--------------------------------");
    StringBuilder Sbuilder = new StringBuilder ("Abcabcabcabcabcabcabcabc");

    System.out.printf ("sbuilder=%s\n", Sbuilder); 1.

    From the point of departure, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.indexof (\" bc\ ")", Sbuilder.indexof ("BC")); 2. Starting from position 5, find out where "BC" first appears System.out.printf ("%-30s =%d\n", "Sbuilder.indexof" ("Bc\", 5) ", Sbuilder.indexof (" BC ", 5)

    ); 3.

    From the back forward, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.lastindexof (\" bc\ ")", Sbuilder.lastindexof ("BC")); 4. Starting from position 4, from the back forward, find out the first occurrence of "BC" Position System.out.printf ("%-30s =%d\n", "Sbuilder.lastindexof (\" Bc\ ", 4)", Sbuilder.lastindexof

    ("BC", 4));
  System.out.println (); /** * StringBuilder replace () example/private static void Testreplaceapis () {System.out.println ("--------

    ------------------------Testreplaceapis------------------------------"); StRingbuilder Sbuilder;
    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.replace (0, 3, "ABCDE");

    System.out.printf ("sbuilder=%s\n", Sbuilder);
    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.reverse ();

    System.out.printf ("sbuilder=%s\n", Sbuilder);
    Sbuilder = new StringBuilder ("0123456789");
    Sbuilder.setcharat (0, ' M ');

    System.out.printf ("sbuilder=%s\n", Sbuilder);
  System.out.println (); /** * StringBuilder Delete () example/private static void Testdeleteapis () {System.out.println ("----------

    ----------------------Testdeleteapis-------------------------------");

    StringBuilder Sbuilder = new StringBuilder ("0123456789");
    Deletes the character at position 0, and the remaining character is "123456789".
    Sbuilder.deletecharat (0);
    Deletes the character between position 3 (included) and the position 6 (not included), and the remaining character is "123789".

    Sbuilder.delete (3,6);
    Get SB to start with position 1 string str1 = sbuilder.substring (1);
    Get SB from position 3 (included) to position 5 (excluding) string str2 = Sbuilder.substring (3, 5); Get SB from location 3 (includingTo the string between position 5 (not included), the object being fetched is the Charsequence object, which is transformed into string string str3 = (string) sbuilder.subsequence (3, 5);
  System.out.printf ("sbuilder=%s\nstr1=%s\nstr2=%s\nstr3=%s\n", Sbuilder, str1, str2, STR3); /** * StringBuilder Insert () example/private static void Testinsertapis () {System.out.println ("----------

    ----------------------Testinsertapis-------------------------------");

    StringBuilder Sbuilder = new StringBuilder ();
    Inserts a character array at position 0 sbuilder.insert (0, New char[]{' A ', ' B ', ' C ', ' d ', ' e '}); Inserts a character array at position 0.
    0 indicates the starting position of the character array, and 3 represents the length Sbuilder.insert (0, New char[]{' A ', ' B ', ' C ', ' D ', ' E '}, 0, 3);
    Insert float Sbuilder.insert (0, 1.414f) at position 0;
    Insert a double sbuilder.insert (0, 3.14159d) at position 0;
    Inserts a Boolean Sbuilder.insert (0, true) at position 0;
    Insert Char sbuilder.insert (0, ' \ n ') at position 0;
    Insert int Sbuilder.insert (0, 100) at position 0;
    Insert Long Sbuilder.insert (0, 12345L) at position 0; Insert the StringBuilder object at position 0 Sbuilder.insert (0, new STRIngbuilder ("StringBuilder")); Inserts a StringBuilder object at position 0.
    6 indicates the starting position (including) at position 0 where the object is inserted, 13 is the ending position (excluding) Sbuilder.insert (0, New StringBuilder ("StringBuilder"), 6, 13);
    Inserts a StringBuffer object at position 0.
    Sbuilder.insert (0, New StringBuffer ("StringBuffer")); Inserts a StringBuffer object at position 0.
    6 indicates the starting position (including) at position 0 where the object is inserted, 12 is the ending position (excluding) Sbuilder.insert (0, New StringBuffer ("StringBuffer"), 6, 12);
    Inserts a string object at position 0.
    Sbuilder.insert (0, "String"); Inserts a string object at position 0.
    1 indicates the starting position (including) at position 0 where the object is inserted, and 6 is the ending position (excluding) Sbuilder.insert (0, "0123456789", 1, 6);

    Sbuilder.insert (0, ' \ n '); Inserts an object at position 0.
    Here take HashMap as an example HashMap map = new HashMap ();
    Map.put ("1", "one");
    Map.put ("2", "two");
    Map.put ("3", "three");

    Sbuilder.insert (0, map);
  System.out.printf ("%s\n\n", Sbuilder); /** * StringBuilder Append () example/private static void Testappendapis () {System.out.println ("----------

    ----------------------Testappendapis-------------------------------"); StringBUilder Sbuilder = new StringBuilder ();
    Append character array sbuilder.append (new char[]{' A ', ' B ', ' C ', ' d ', ' e '}); Appends an array of characters.
    0 indicates the starting position of the character array, and 3 represents the length Sbuilder.append (new char[]{' A ', ' B ', ' C ', ' D ', ' E '}, 0, 3);
    Add float sbuilder.append (1.414f);
    Double Sbuilder.append (3.14159d) appended;
    Append Boolean sbuilder.append (TRUE);
    Append char sbuilder.append (' \ n ');
    Append int sbuilder.append (100);
    Append long Sbuilder.append (12345L);
    Append StringBuilder Object Sbuilder.append (New StringBuilder ("StringBuilder")); Appends the StringBuilder object.
    6 indicates the starting position of the appended object (including), 13 is the end position (excluding) Sbuilder.append (new StringBuilder ("StringBuilder"), 6, 13);
    Appends the StringBuffer object.
    Sbuilder.append (New StringBuffer ("StringBuffer")); Appends the StringBuffer object.
    6 indicates the starting position of the appended object (including), 12 is the end position (excluding) Sbuilder.append (new StringBuffer ("StringBuffer"), 6, 12);
    Appends a string object.
    Sbuilder.append ("String"); Appends a string object.
    1 indicates the starting position of the appended object (including), 6 is the end position (excluding) sbuilder.append ("0123456789", 1, 6);Sbuilder.append (' \ n '); Append Object object.
    Here take HashMap as an example HashMap map = new HashMap ();
    Map.put ("1", "one");
    Map.put ("2", "two");
    Map.put ("3", "three");
    Sbuilder.append (map);

    Sbuilder.append (' \ n ');  Append Unicode encoding Sbuilder.appendcodepoint (0x5b57);  0x5b57 is the "word" of the Unicode Encoding Sbuilder.appendcodepoint (0X7B26);  0X7B26 is the Unicode encoding Sbuilder.appendcodepoint (0X7F16) of "character";  0x7f16 is a "codec" of the Unicode Encoding Sbuilder.appendcodepoint (0x7801);
  0x7801 is the "code" of the Unicode Encoding System.out.printf ("%s\n\n", Sbuilder);

 }
}

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.