Tidying Up Java Basics--string class

Source: Internet
Author: User

Java String class
Create a string
The simplest notation: string i = "I am a string";
The string class is immutable, and once a string object is created, it is fixed

String i = "123";System.out.println("i = " + i);i = "abc";System.out.println("i = " + i);输出结果:123abc

equivalent to :

String i = new String("123");System.out.println("i = " + i);String i = new String("abc");System.out.println("i = " + i);i & 123 & abc 需要分配3个单位的内存单元 程序自上而下执行,只是先后把123 & abc 赋值给i 并输出


From the output, I is changing the fact that 123 ABC has not changed in memory. And I just reference the object itself has not changed
Connection string
1.concat method joins: String1.concat (string2)
2. Use + Join

class TestA{    public static void main(String[] args){        String string1 = "123";        String string2 = "abc";        String string3 = "llll";        String string4;        String string5;        string4 = string1.concat(string2).concat(string3);        string5 = "123" + "abc" + "llll";        System.out.println("string4 = " + string4);        System.out.println("string5 = " + string5);    }}输出结果:string4 = 123abcllllstring5 = 123abcllll

Common methods of the string class
1. Length of the string
public int Length ()

String str = new String("adzzbzz");int len = str.length();//len = 7

2. Characters at the string index
public char charAt (int index)

String str = new String("adzzbzz");char ch = str.charAt(5);//ch = b

3. String comparisons
int CompareTo (Object o)
Or
int CompareTo (String anotherstring)

String str1 = new String("abc");String str2 = new String("ABC");int a = str1.compareTo(str2);//a>0int b = str1.compareTo(str2)//b=0

4. String connection
public string concat (string s)

String str = "111".concat("222").concat("333");String str = "111"+"222"+"333";

5. Extracting substrings
Public String substring (int beginindex)
Public String substring (int beginindex, int endIndex)

String str1 = new String("adzzbzz");String str2 = str1.substring(2);//str2 = "zzbzz"String str3 = str1.substring(2,5);//str3 = "zzb"

6. Single character lookup in a string
public int indexOf (int ch/string str)
public int indexOf (int ch/string str, int fromIndex)
public int lastIndexOf (int ch/string str)
public int lastIndexOf (int ch/string str, int fromIndex)

String str = "I am a good student";int i1 = str.indexOf(‘a‘);//i1= 2int i2 = str.indexOf("good");//i2 = 7int i3 = str.indexOf("w",2);//i3= -1int i4 = str.lastIndexOf("a");//i4= 5int i5 = str.lastIndexOf("a",3);//i5= 2

7. Case conversion of characters in a string
Public String toLowerCase ()
Public String toUpperCase ()

String str = new String("abCD");String str1 = str.toLowerCase();//str1 = "abcd"String str2 = str.toUpperCase();//str2 = "ABCD"

8. Substitution of characters in a string
Public String replace (char OldChar, char Newchar)
public string Replacefirst (string regex, string replacement)
public string ReplaceAll (string regex, string replacement)

String str = "adzzzad";String str1 = str.replace(‘a‘,‘b‘);//str1 = "bdzzzbd"String str2 = str.replace("ad","bp");//str2 ="bpzzzbp"String str3 = str.replaceFirst("ad","bp");//str3 = bpzzzad"String str4 = str.replaceAll("ad","bp");//str4 = "bpzzzbp"

Organizing Java Basics--string class

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.