Java basic notes-String class 2, java basic notes-string

Source: Internet
Author: User

Java basic notes-String class 2, java basic notes-string

StringBuffer

Features:

It is modified by the final lock and therefore cannot be inherited.

 

Storage:

Method 1: append () adds data to the buffer.

Return type: StringBuffer

Method: append adds the specified data to the end of the existing data.

 

Method 2: insert (index, data content)

Return type: StringBuffer

Method: insert (index, data content) inserts data into the specified index position.

 

Delete:

Method 1: delete (int start, int end) deletes the data in the buffer.

Return type: StringBuffer

Method: delete (int start, int end) deletes data from start to end.

The include header does not contain the tail, that is, the start position. It does not contain the end position.

Method 2: deletecharAt (index) deletes the character at the specified index position

 

Replace:

Method 1: replace (location 1, Location 2, (String type)

The return type is StringBuffer.

Method: replace (location 1, Location 2, (String type)

Replace the data from location 1 to location 2 (excluding Location 2) with the string content to be replaced.

Method 2: setCharAt (location, content to be replaced)

Return type void

Method: setCharAt (int position, char content to be replaced) replaces the data with the character content from the specified position.

 

Reverse: reverse () returns the StringBuffer type.

Related code examples:

1 class StringBufferDemo 2 {3 public static void main (String args []) 4 {5 // function_add (); 6 // function_del (); 7 StringBuffer sb = new StringBuffer ("abcdefg"); 8 char [] ch = new char [6]; 9 sb. getChars (, ch, 1); 10 for (int I = 0; I <= ch. length; I ++) 11 {12 printer ("ch [" + I + "] =" + ch [I] + ";"); 13} 14} 15 public static void function_del () 16 {17 StringBuffer sb = new StringBuffer ("abcdefg"); 18 19 // sb. Delete (0, sb. length (); // clear the buffer. 20 21 sb. delete (5.22); // delete the data from string location 2 to 5 in the buffer, excluding the position printer (sb. toString (); 23 // sb. delete (1, 2); 24 sb. deleteCharAt (1); // delete a data, equivalent to the above Code: sb. delete (1, 2); 25 26 printer (sb. toString (); 27 28} 29 public static void function_add () 30 {31 StringBuffer sb = new StringBuffer (); 32 sb. append ("abc "). append ("def "). append (true ). append (123); 33 34 // StringBuffer sb1 = sb. append (34 ); 35 36 sb. insert (1, "QQQ"); // insert the string "QQQ" from position 1 to the data in the buffer zone. 37 38 // sb. insert (100, "QQQ"); An exception occurs during compilation. 39 // printer ("sb = sb1?: "+ (Sb = sb1); 40 41 printer (sb. toString (); 42 // printer (sb1.toString (); 43} 44 45 public static void printer (String str) 46 {47 System. out. println (str); 48} 49}

 

JDK1.5StringBuilder is available after version

StringBufferIt is thread synchronization.

StringBuidlderYes, the thread is not synchronized, and the efficiency is high for a single thread.

We recommend that you use StringBuilder for future development.

JDKUpgrade generally involves three aspects:

 

Basic Data Type object packaging class:

Lowercase is the basic data type.

Int Integer

Byte Byte

Float Float

Double Double

Boolean Boolean

Char Character

Short Short

Long Long

 

1. obtain the maximum Integer value: Integer. MAX_VALUE

Example: printer ("int max =" + Integer. MAX_VALUE );

 

The most common applications are:

Convert the basic data type to a string.

Method 1: Basic Data Type + ""

Method 2: Basic data type class. toString (basic data type)

Example: Integer. toString (34); Convert Integer 34 to string type "34 ".

 

Convert a string to a basic data type

Convert a string to an integer:

(Static call method)

Integer. parseInt ("32"); Convert string "32" to Integer 32.

 

Format:

Xxx a = Xxx. parseXxx (String );

Example: int a = Integer. parseInt ("123 ");

 

Non-static method conversion (object call method)

Integer a = new Integer ("123 ");

Int num = a. intValue ();

 

Conversion between hexadecimal:

Convert decimal to other hexadecimal formats:

Integer. toBinayString (number to be converted); // convert to binary

Integer. toHexString (number to be converted); // convert to hexadecimal

Integer. toOctalString (number to be converted); // converts it to octal.

Convert other hexadecimal values to decimal values:

Format:

Xxx a = Xxx. parseXxx (number to be converted, in hexadecimal format)

For example: int a = Integer. parseInt ("110", 2); Convert 110 to a decimal number using binary.

The numbers in the brackets and the hexadecimal conversion must correspond to each other. Otherwise, an exception occurs during compilation.

Simple code example:

1 class IntegerDemo 2 {3 public static void main (String args []) 4 {5 // obtain the maximum value of the Integer variable 6 printer ("int max =" + Integer. MAX_VALUE); 7 8 int num = Integer. parseInt ("123"); // convert string "123" to an integer of 123. 9 num = num + 4; // Add 10 printer ("num =" + num) to Integer 4 and Integer 123 First; // The output result is 127.11 12 printer (Integer. toBinaryString (-6); // convert the decimal number to the binary number 13 printer (Integer. toHexString (60); // convert the decimal number to the hexadecimal number 14 printer (Integer. toOctalString (60); // convert the decimal number to octal number 15 16 int a = Integer. parseInt ("110", 2 ); // convert the binary number 110 to the decimal number in binary number 110. // The decimal number corresponding to the binary number 6.17 is printer ("a =" + ); // result a = 6.18 19} 20 21 public static void printer (String str) 22 {23 System. out. println (str); 24} 25 26 27}
1 class IntegerDemo1 2 {3 public static void main (String args []) 4 {5 // function (); 6 // Integer a = new Integer (4 ); 7 Integer a = 4; // automatically boxed 8 a = a + 2; // The a phase in a + 2 is. in the intValue () operation, a Automatically splits the box into the int type. then perform the operation with 2 and assign the auto-boxing operation to. 9 printer ("a =" + a); 10 11 12 Integer x = 128; 13 Integer y = 128; 14 printer ("x = y :? "+ (X = y); // The result is false.15 16 Integer m = 127; 17 Integer n = 127; 18 printer (" m = n :? "+ (M = n); // The result is true. m and n point to the same Integer Object 19 // when the value is within the byte range, for new versions later than JDK1.5, if the value already exists, it will not open up new space. 20 21} 22 public static void function () 23 {24 Integer x = new Integer ("123"); 25 Integer y = new Integer (123 ); 26 printer ("x = y :? "+ (X = y); 27 printer (" x. equals (y ):? "+ X. equals (y); 28} 29 public static void printer (String str) 30 {31 System. out. println (str); 32} 33 34 35}

 

Related Article

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.