java中String类的常用功能以及方法

一. 构造方法:

1. String(String original);

功能:把字符串数据封装成字符串对象;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = new String("helloworld");
		System.out.println(s);
	}
}

java中String类的常用功能以及方法
注意:字符串是一种比较特殊的引用数据类型,直接输出字符串对象输出的是该对象中的数据。

2. String(char[] value);

功能:把字符数组的数据封装成字符串对象
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		char[] str = {'h','e','l','l','o'};
		String s = new String(str);
		System.out.println(s);
	}
}

java中String类的常用功能以及方法

3. String(char[] value, int index, int count);

功能:把字符数组中的一部分数据封装成字符串对象,即:将字符数组中以索引为index开头以及后面的数据,共count个数据封装成字符串对象;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		char[] str = {'h','e','l','l','o'};
		String s = new String(str,1,3);
		System.out.println(s);
	}
}

java中String类的常用功能以及方法

二. String类的判断功能:

1. boolean equals(Object obj);

功能:比较字符串的内容是否相同;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "hello";
		String s3 = "Hello";
		System.out.println(s1.equals(s2));
		System.out.println(s1.equals(s3));
	}
}

java中String类的常用功能以及方法

2. boolean equalsIgnoreCase(String str);

功能:比较字符串的内容是否相同,忽略大小写;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "hello";
		String s3 = "HELLO";
		String s4 = "helll";
		System.out.println(s1.equalsIgnoreCase(s2));
		System.out.println(s1.equalsIgnoreCase(s3));
		System.out.println(s1.equalsIgnoreCase(s4));
	}
}

java中String类的常用功能以及方法

3. boolean startsWith(String str);

功能:判断字符串对象是否以指定的str开头;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "he";
		String s3 = "el";
		System.out.println(s1.startsWith(s2));
		System.out.println(s1.startsWith(s3));
	}
}

java中String类的常用功能以及方法

4. boolean endsWith(String str);

功能:判断字符串对象是否以指定的str结尾;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "llo";
		String s3 = "ll";
		System.out.println(s1.endsWith(s2));
		System.out.println(s1.endsWith(s3));
	}
}

java中String类的常用功能以及方法

三. String类的获取功能:

1. int length();

功能:获取字符串的长度,即字符个数;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		System.out.println(s1.length());
	}
}

java中String类的常用功能以及方法

2. char charAt(int index);

功能:获取指定索引处的字符;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "hello";
		for(int i=0;i<s1.length();i++) {
			System.out.println(s1.charAt(i));
		}
	}
}

java中String类的常用功能以及方法

3. int indexOf(String str);

功能:获取str在字符串对象中第一次出现的索引;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = "hello";
		System.out.println(s.indexOf("h"));
		System.out.println(s.indexOf("e"));
		System.out.println(s.indexOf("l"));
		System.out.println(s.indexOf("l"));
	}
}

java中String类的常用功能以及方法

4. String substring(int start);

功能:从start开始截取字符串;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = "hello";
		System.out.println(s.substring(1));
		System.out.println(s.substring(3));

	}
}

java中String类的常用功能以及方法

5. String substring(int start,int end);

功能:从start开始,到end结束截取字符串。包括start,不包括end
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = "hello";
		System.out.println(s.substring(1,2));
		System.out.println(s.substring(3,s.length()));

	}
}

java中String类的常用功能以及方法

四. String类的转换功能:

1. char[] toCharArray();

功能:把字符串转换为字符数组;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = "hello";
		char[]  arr = s.toCharArray();
		System.out.println(arr);
		for(int i=0;i<arr.length;i++) {
			System.out.print(arr[i]);
		}
	}
}

java中String类的常用功能以及方法

2. String toLowerCase();

功能:把字符串转换为小写字符串;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "HelloWorld";
		String s2 = s1.toLowerCase();
		System.out.println(s1);
		System.out.println(s2);
	}
}

java中String类的常用功能以及方法

3. String toUpperCase();

功能:把字符串转换为大写字符串;
示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "HelloWorld";
		String s2 = s1.toUpperCase();
		System.out.println(s1);
		System.out.println(s2);
	}
}

java中String类的常用功能以及方法

五. 去除字符串两端空格 :

1. String trim();

示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s1 = "     Hel lo  World    ";
		String s2 = s1.trim();
		System.out.println("----"+s1+"----");
		System.out.println("----"+s2+"----");
	}
}

java中String类的常用功能以及方法

六. 按照指定符号分割字符串:

1. String[] split(String str);

示例:

package string.demo_5;

public class test03 {
	public static void main(String[] args) {
		String s = "12,34,56,78";
		String[] arr = s.split(",");
		for(int i=0;i<arr.length;i++) {
			System.out.println(arr[i]);
		}
	}
}

java中String类的常用功能以及方法
注意:使用"|“或者”.“去分割某字符时,因 “|“或者”.“本身是正则表达式中的一部分,所以需要” \” 去转义,因转义使用”\", 而这个"\“正好也是正则表达式的字符,所以还得用一个”\", 所以需要两个即:"\\"。
所以使用时应该写为"\\.“或者”\\|"。