JAVA Foundation 5-string

Source: Internet
Author: User

I. String definitions

Direct definition: String str1 = "abc"; string str2 = "abc";

Creates a new string: string str3 = new string (); String STR4 = new string ();

A string type is a reference type, and the variable that is defined is an object that is written in the same object-oriented way.

Note: str1 = = STR2 The return value is true, because the value of ABC is in the constant pool of memory, and when it is defined again, str1 and str2 point to the same value.

STR3 = = STR4 The return value is false, because new is used, two spaces are created in memory, and STR3 and STR4 point to different objects respectively.

String str3 = new String ("ABC") is equivalent to string STR3 = "abc", but the underlying operating mechanism is different.

String str1 = null and string str2 = "" Does not mean the first one has no point, the second point is empty.

Two. String connection

Each string is concatenated with "+", and when connected to another data type, the return value after the connection is a string.

Three. Common string manipulation methods

String str = "ABC"; String str2 = "abc"

  • Str.equals (STR2); Compares the values of two strings for equality, (= = when used for non-underlying data types refers to comparing two variables to the same address, and equals compares two objects for equal values)
  • Str.length ();//Gets the length of the entire string;
  • Str.trim ();//Remove whitespace on both sides of the string;
  • Str.charat (int i);//Gets the character on an index value;
  • Str.contains (Charsequence AB);//Whether a string is included; Charsequence character sequence
  • Str.startswith (String AB); Determines whether to start with a string;
  • Str.endswith (String AB); Determines whether to end with a string;
  • Str.replace (char A, char e); Replaces all a in a string with an E;
  • Str.replace (charsequence AB, charsequence AA);//Replace All AB in the string with AA;
  • Str.split (String s); Splits the string, splits the string into S, and the return value is an array;
  • Str.touppercase (); Convert a string to uppercase
  • Str.tolowercase (); Convert to lowercase
  • Str.indexof (String ab);//The index position at which this string appears for the first time
  • Str.lastindexof (string s);//Take the index where this string last appears
  • str.substring (int i);//The index value is the string following the integer argument
  • str.substring (int i, int j);//Take the string between I and J (excluding J)
  • String.valueof (any args); Converts any argument to a string, which is a method of a class and belongs to a static method.

Four. Common string buffer operation methods (mainly used to build strings)

The return value is generally stringbuffer, but it is not recommended to perform a class-coherent operation.

StringBuffer buffer = new StringBuffer ("abc")

    • Buffer.append (String 123); Append a string;
    • Buffer.insert (int 1, String 123); Inserts a string at an indexed position; output is A123BC
    • Buffer.deletecharat (int index);//delete the character at an index position;
    • Buffer.delete (int start, int end);//delete the characters between two indexes;
    • Buffer.replace (int start, int end, String s);//replace the characters between two indexes;
    • Buffer.setcharat (int index, char ch); Sets the character at an index position;
    • Buffer.tostring (); Converts a string buffer to a string; This method is called by default when printing output;
    • Buffer.reverse ()//Invert string

Note: The difference between a string and a string buffer is that the string is constant and cannot be changed, and its efficiency is much lower than using string buffering when doing the operation;

Five. System class

    • System.currenttimemillis ();//Returns the current system time (in the form of a timestamp)
    • System.exit (0);//exit the currently running Java Virtual machine, 0 indicates normal exit, the other is not normal exit, usually with-1, indicating that the whole is not running.
    • System.GC ();//Manually start the garbage collection mechanism; Java automatically start the garbage collection mechanism, objects beyond the scope of the variables, will be in a timely manner by the garbage collection mechanism. Calling this method manually (reducing program run efficiency) is not recommended, and this method automatically calls the Finalize method.
    • System.arraycopy (object src, int srcpos, object dest, int destpos, int length);//copy of array, referring to copy source name, source start copy index location, copy target name, The target begins copying the index location, copying the length.

Six. Digital Packaging class

Automatic Boxing:

The underlying data type cannot be equal to NULL, it can be wrapped, and each underlying data type has its own wrapper class.

The int corresponds to the integar;char corresponding to the character; the other is the first capitalization example: long corresponds to a long; wrapping is an object that can be equal to null.

Integar i = 2; This code allows the program to complete the automatic boxing of the variable i;

Automatic unpacking:

    • Integar.parseint ("123");//Converts the string 123 to a number 123, or an error if there are non-numeric characters in the string;
    • Integer.parseint (String s, int radix); Converts the other binary string to int, and the comma after the converted binary, such as write 16 is the 16 binary string into the 10 binary; radix: cardinality;
    • integer.tobinarystring (int i); Converts an int value into a binary string
    • integer.tooctalstring (int i); Converts an int to an octal string
    • integer.tohexstring (int i); Converts an int to a 16-binary string

JAVA Foundation 5-string

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.