String types in Java

Source: Internet
Author: User

String types in Java
(1) A string type of data can represent all data types.

(2) string constants and generic strings in string:
string str0 = "Hello";//String constant "Hello" is pre-placed in the string constant pool of the data segment
String str1 = "Hello";//search for existing string constants directly from the constant pool
String str2 = new String ("Hello");//new a new string object in the heap
String str3 = new String ("Hello");//new a new string object in the heap
System.out.println (STR0 = = str1);//Print True
System.out.println (str2 = = STR3);//Print False
System.out.println (Str2.equals (STR3));//Print True

//string Type Object (whether it is a constant pool object or an object in the heap, its contents are immutable)
STR3 = str2;
STR2 = "World";
System.out.println (STR3);//print is still the string hello
(3) for string str2 = new String ("Hello"), two string objects (Hello and str) are generated, resulting in three objects (two string objects, one class object).

(4) methods commonly used in strings:

Strings compare Equals, Equalsignorecase, CompareTo, comparetoignorecase

System.out.println ("Hello". Equals ("Hello"));//Determines whether two strings are equal-----print true

System.out.println ("Hello". Equalsignorecase ("Hello"));//Ignore case comparison equal---print true

System.out.println ("Hello". CompareTo ("World"));//string comparison: First compares the ASCII code difference of the first different letter in turn, If all the same is longer than the string length----print-15

System.out.println ("Hello". Comparetoignorecase ("HelloWorld"));

A method in a string that is related to a character array

System.out.println (Str0.length ());//String length

System.out.println (Str0.charat (0));//What character is the first position of the string

char[] Strarray = Str0.tochararray ();//Convert a string to a character array
for (char Tmp:strarray) {
SYSTEM.OUT.PRINTLN (TMP);
}

Byte[] B = str0.getbytes ();//Convert a string to a byte array

System.out.println (Str0.indexof (' l '));//The subscript for the first occurrence of a character in a string

System.out.println (Str0.indexof (' l ', 3));//A string starting with the specified subscript to see the first occurrence of a character subscript.

System.out.println (Str0.lastindexof (' l '));//The last occurrence of a character in a string

String content-related methods

System.out.println (Str0.touppercase ());//Convert to full capitalization

System.out.println (Str0.tolowercase ());//convert to full lowercase

System.out.println (Str0.startswith ("wor"));//Determine what the string begins with

System.out.println (Str0.endswith ("Lo"));//Determine what the string ends with

System.out.println (Str0.contains ("ell"));//Determine if a string is in another string--contains

System.out.println (Str0.concat ("World"));//Add a string to the end of another string

System.out.println (Str0.replace (' l ', ' l '));//Replace a character in a string with a new character

System.out.println (Str0.replace ("L", "fuck"));//Replace a substring in a string with a new substring

System.out.println (str0.substring (1,3));//The string is intercepted by position, and the opening interval of the front closed

System.out.println (str0.substring (2));//The string is intercepted from this position.

The trim () method in the string:

System.out.println ("Hello World". Trim ());//Remove space before and after ————-print result: Hello World


The split () method in the string:

String date = "2016-03-02";
String[] result = Date.split ("-"); the parameter in//split is a regular expression. A simple? should be written as "\ \?"
for (String A:result) {
System.out.println (a);
}


Operation Result:
2016
03
02


If date = "03-02", the result of printing is:
03
02
The length of the result at this time is still 3, but one is an empty string. String a = "";(empty string) string a = null; (empty)

So judging if the string is valid: if (str!) = NULL &&! (Str.trim (). Equals ("")))----Two positions cannot be swapped, otherwise a null pointer exception will be reported.

String types in Java

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.