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

猜你喜欢

转载自blog.csdn.net/heng_guofen/article/details/82860106