1. 程式人生 > >Java String類的基本用法

Java String類的基本用法

一、String類物件的建立
字串宣告:String stringName;
字串建立:stringName = new String(字串常量);或stringName = 字串常量;
二、String類構造方法
1、public String()
無參構造方法,用來建立空字串的String物件。

String str1 = new String(); 

2、public String(String value)
用已知的字串value建立一個String物件。

String str2 = new String("asdf"); 
String str3 = new String(str2); 

3、public String(char[] value)
用字元陣列value建立一個String物件。

 char[] value = {"a","b","c","d"};
 String str4 = new String(value);//相當於String str4 = new String("abcd");

4、public String(char chars[], int startIndex, int numChars)
用字元陣列chars的startIndex開始的numChars個字元建立一個String物件。

char[] value = {"a","b","c","d"};
String str5 = new String(value, 1, 2);//相當於String str5 = new String("bc");

5、public String(byte[] values)
用位元陣列values建立一個String物件。

 byte[] strb = new byte[]{65,66};
 String str6 = new String(strb);//相當於String str6 = new String("AB");

三、String類判斷是否相等,“==”是用來判斷兩個字串(物件)的地址是否相同,“equals()”是用來判斷兩個字串(物件)的值是否相等,如果相等則返回true,否則返回false。

package blog;

public class HelloWorld {
	public static void main(String[] args) {
	
		String a = "helloworld";
		String b = "helloworld";
		System.out.println(a.equals(b)); 
	}
}//true
public class HelloWorld {
	public static void main(String[] args) {
	
		String a = "helloworld";
		String b = "hello";
		System.out.println(a.equals(b)); 
	}
}//false

一般情況下,都是使用“equals()”來判斷兩個字串的值是否相等,只有當你需要判斷兩個字串是否是同一個物件時,才使用“==”。

public class HelloWorld {
	public static void main(String[] args) {
		   
			char[] a = {'h','e','l','l','o'};       
			String t1 = "hello";       
			String t2 = new String("hello");      
			String t3 = new String(a);       
			String t4 = t3;       
			String t5 = "hello";        
			System.out.println(t1==t2);//false       
			System.out.println(t2==t3);//false       
			System.out.println(t3==t4);//true       
			System.out.println(t5==t1);//true        
			System.out.println(t1.equals(t1));//true      
			System.out.println(t1.equals(t2));//true        
			System.out.println(t1.equals(t3));//true        
			System.out.println(t1.equals(t4));//true       
			System.out.println(t1.equals(t5));//true    
			}
		}

四、String類常用方法
1.獲取字串的子串
用String類的substring方法可以提取字串中的子串,該方法有兩種常用引數:
1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字串中取出剩餘的字元作為一個新的字串返回。
2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字串中取出到endIndex-1位置的字元作為一個新的字串返回。

public String substring(int beginIndex)
//該方法從beginIndex位置起,
//從當前字串中取出剩餘的字元作為一個新的字串返回。

public String substring(int beginIndex, intendIndex)
//該方法從beginIndex位置起,從當前字串中
//取出到endIndex-1位置的字元作為一個新的字串返回。
String str1 = newString("asdfzxc");
String str2 = str1.substring(2);//str2 ="dfzxc"
String str3 = str1.substring(2,5);//str3 ="dfz"

2.字串連線
public String concat(String str)//將引數中的字串str連線到當前字串的後面,效果等價於"+"。

String str = "aa".concat("bb").concat("cc");

相當於

String str = "aa"+"bb"+"cc";

3.求字串長度

public int length()//返回該字串的長度

 String str = new String("abcdef");
 int strlength = str.length();//strlength = 6

4.求字串某一位置字元

public char charAt(int index)//返回字串中指定位置的字元;注意字串中第一個字元索引是0,最後一個是length()-1。

 String str = new String("asdfzxc");
 char ch = str.charAt(4);//ch = z

參考資料: https://blog.csdn.net/major_zhang/article/details/54912040
https://www.cnblogs.com/ABook/p/5527341.html
https://blog.csdn.net/qq_25406669/article/details/79021911