String类的常用功能

String类的常用构造方法 

/*
 * String类的常用构造方法
 * 		public String():空构造
 * 		public String(byte[] bytes):把字节数组转换成字符串
 * 		public String(byte[] bytes,int index,int length):把字节的一部分转换成字符串
 * 	 	public String(char[] value):把字符数组转换成字符串
 * 		public String(char[] value,int index,int count):把字符的一部分转换成字符串
 * 		public String(String original):把字符串常量值转成字符串
 * 	字符串的方法:
 * 		public int length():返回此字符串的长度。
 */

public class StringDemo {
	public static void main(String[] args) {
		// 字节数组
		byte[] b = { 97, 98, 99, 100, 101 };
		// public String(byte[] bytes):把字节数组转换成字符串
		String s = new String(b);
		System.out.println(s);
		System.out.println("------");

		// public String(byte[] bytes,int index,int length):把字节的一部分转换成字符串
		String s2 = new String(b, 0, 2);
		System.out.println(s2);
		System.out.println("------");

		// 字符数组
		char[] c = { '保', '护', '刘', '波', '?' };
		// public String(char[] value):把字符数组转换成字符串
		String s3 = new String(c);
		System.out.println(s3);
		System.out.println("------");

		// public String(char[] value,int index,int count):把字符的一部分转换成字符串
		String s4 = new String(c, 2, 2);
		System.out.println(s4);
	}
}

 String类的判断功能:

/*
 * String类的判断功能:
 * 		boolean equals(Object obj);比较两个字符串的内容是否相同,区分大小写
 *      boolean equalsIgnoreCase(Object obj);比较两个字符串的内容是否相同,忽略大小写
 * 		boolean contains(String str);判断大字符串中是否包含小字符串
 *      boolean startWith(String str);判断字符串是否以某个指定的字符串开头
 *    	boolean endsWith(String str);判断字符串是否以某个指定的字符串结尾
 *  	boolean isEmpty();判断字符串是否为空
 *  
 *  注意
 *  	字符串对象为空和字符串内容为空不是一个概念
 *     
 */

public class StringDemo2 {
	public static void main(String[] args) {
        String str1 = "helloworld";
        String str2 = "helloworld";
        String str3 = "HelloWorld";

        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println("----------");

        System.out.println(str1.equalsIgnoreCase(str2));
        System.out.println(str1.equalsIgnoreCase(str3));
        System.out.println("----------");

        System.out.println(str1.endsWith(str2));
        System.out.println(str1.endsWith(str3));
        System.out.println("----------");

        System.out.println(str1.startsWith(str2));
        System.out.println(str1.startsWith(str3));
        System.out.println("----------");
    }
}



/*结果:
    true
    false
    ----------
    true
    true
    ----------
    true
    false
    ----------
    true
    false
    ----------
*/

String类的获取功能 

/*
 * String类的获取功能
 *  	int length();获取字符串的长度
 *		char charAt(int index);获取指定索引位置的字符
 * 		int indexOf(int ch);获取指定字符在此字符串中第一次出现处的索引
 *		int indexOf(String str);获取指定字符串在此字符串中第一次出现处的索引
 *		int indexOf(int ch, int fromIndex);获取指定字符在此字符串中从指定位置后第一次出现处的索引
 *		int indexOf(String str, int fromIndex);获取指定字符串在此字符串中从指定位置后第一次出现处的索引
 *		String subString(int start);从指定位置开始截取字符串,默认到结尾位置
 *		String substring(int start, int end);从start位置开始截取字符串,到end位置结束。不包括end位置的字符
 *
 */

public class StringDemo3 {
	public static void main(String[] args) {
		// 定义一个字符串对象
		String str = "HelloWorld";

		// int length();获取字符串的长度
		System.out.println(str.length());

		// char charAt(int index);获取指定索引位置的字符
		System.out.println(str.charAt(5));

		// int indexOf(int ch);获取指定字符在此字符串中第一次出现处的索引
		System.out.println(str.indexOf("l"));

		// int indexOf(String str);获取指定字符串在此字符串中第一次出现处的索引
		System.out.println(str.indexOf("World"));

		// int indexOf(int ch, int fromIndex);获取指定字符在此字符串中从指定位置后第一次出现处的索引
		System.out.println(str.indexOf("l", 2));

		// int indexOf(String str, int fromIndex);获取指定字符串在此字符串中从指定位置后第一次出现处的索引

		// String subString(int start);从指定位置开始截取字符串,默认到结尾位置
		System.out.println(str.substring(5));

		// String substring(int start, int end);
		// 从start位置开始截取字符串,到end位置结束。不包括end位置的字符
		System.out.println(str.substring(2, 6));
	}
}

字符串的遍历 

/*
 * 字符串的遍历
 */

public class StringDemo4 {
	public static void main(String[] args) {
		// 定义一个字符串对象
		String str = "保护刘波";

		for (int x=0; x < str.length(); x++) {
			System.out.println(str.charAt(x));
		}

	}
}

String类的转换功能 

/*
 * String类的转换功能
 * 		byte[] getBytes():将一个字符串转换字节数组 
 * 		char[] toCharArray():(重点方法):将一个字符串转换成字符数组 
 *		static String valueOf(char[] chs):将字符数组转换成字符串 
 *		static String valueOf(int i):将int类型的数据转换字符串 
 *			注意:String类中的valueOf方法可以将任何数据类型转换成String 
 *		String toLowerCase():将字符串转换成小写 
 *		String toUpperCase():将字符串转换成大写 
 *		String concat(String str):字符串拼接功能
 *注意:
 *		数组中有没有length()? 字符串中有没有length()? 
 *		length:数组长度属性 :数组中不存在这个方法 
 *		字符串中有length():返回字符串长度
 */

public class StringDemo5 {
	public static void main(String[] args) {
		String str = "保护刘波";
		String str2 = "HelloWorld";

		// byte[] getBytes():将一个字符串转换字节数组
		byte[] b = str2.getBytes();
		for (byte bb : b) {
			System.out.println(bb);
		}

		// char[] toCharArray():(重点方法):将一个字符串转换成字符数组
		char[] b2 = str.toCharArray();
		for (char bb : b2) {
			System.out.println(bb);
		}

		// String toLowerCase():将字符串转换成小写
		System.out.println(str2.toLowerCase());

		// String toUpperCase():将字符串转换成大写
		System.out.println(str2.toUpperCase());

		// String concat(String str):字符串拼接功能
		System.out.println(str.concat(str2));
	}
}

 String类的其他功能 

/*
 * String类的其他功能
 * 替换功能:
 * 		String replace(char old,char new):
 * 		String replace(String old,String new):
 * 
 * 去除字符串两空格(去前后两端空格):
 * 		String trim():
 * 
 * 按字典顺序比较两个字符串
 * 		int compareTo(String str):
 * 		int compareToIgnoreCase(String str):
 *
 */

public class StringDemo6 {
	public static void main(String[] args) {
		String str = "保护刘波";
		String str2 = "HelloWorld";

		// String replace(char old,char new):
		System.out.println(str.replace("刘波", "孙笑川"));
		
		

	}
}

猜你喜欢

转载自blog.csdn.net/qq_41690324/article/details/81293269