Java中的String类常用用法

String类的基础用法:

public class String类的基础用法 {
    
    
	public static void main(String arg[]){
    
    
		System.out.println("------创建字符串---------");
		String s01 = new String("hello");

		System.out.println("------字节数组转换为字符串---------");
		byte[] byteArray = {
    
    97,98,99};
		String s02 = new String(byteArray);
		System.out.println(s02);

		System.out.println("------字符数组转换为字符串---------");
		char[] charArray1 = {
    
    'a','b','c'};
		String s03 = new String(charArray1);
		System.out.println(s03);

		System.out.println("------使用字符数组的一部分构建字符串对象---------");
		char[] charArray2 = {
    
    'a','b','c','d','e'};
		//charArray2后面的2为偏移字符下标,1为偏移的量
		String s04 = new String(charArray2,2,1);
		System.out.println(s04);

		System.out.println("------length获取字符串长度---------");
		String s = "abc";
		System.out.println(s.length());   //3

		System.out.println("------字符串转为字符数组---------");
		char[] arr=s03.toCharArray();
		System.out.println(arr);
		for(char ch:arr) {
    
    
			System.out.println(ch);
		}

		System.out.println("------字符串转为字节数组---------");
		byte[] bytes = s02.getBytes();
		for(byte b:bytes) {
    
    
			System.out.println(b);
		}

		//小写转大写
		String s05 = "hello";
		String upperCase = s05.toUpperCase();
		//大写转小写
		String s06 = "ABC";
		String lowerCase = s06.toLowerCase();

		System.out.println("------字符串和任意类型之间使用"+"---------");
		int age= 6;
		String s6="He is" + age + "years old.";
		//s整个都是字符串型
		System.out.println(s6);

		System.out.println("------trim修剪---------");
		//trim (修剪)
		String s7 = "   xs  ";
		String newString = s7.trim();

		System.out.println("------判断---------");
		String s07 = "asd";
		String s08 = "ASD"; 
		//判断两个字符串是否相同
		System.out.println(s07.equals(s08));  //false
		//判断两个字符串是否相同(忽略大小写)
		System.out.println(s07.equalsIgnoreCase(s08));  //true
		String s09 = "abcdef";
		String s10 = "abc";
		//判断字符串s09是否以s10开头
		System.out.println(s09.startsWith(s10));  //true
		//判断字符串s09是否以s10结尾
		System.out.println(s09.endsWith(s10));  //false
		//判断字符串s11是否为空
		String s11 = " ";
		System.out.println(s11.isEmpty());  //false

		System.out.println("------定位---------");
		String s15 = "asdfghj";
		//返回指定索引的字符
		char ch = s15.charAt(2);  //d
		String s16 = "zxcvxbnxm";
		//返回此字符在字符串中第一次出现的索引值,如果都不匹配则返回-1
		int index = s16.indexOf('v');   //3
		index = s16.indexOf(2);   // -1
		//返回此字符在字符串中最后一次出现的索引值,如果都不匹配则返回-1
		index = s16.lastIndexOf('x');  //7
		index = s16.lastIndexOf(3);  //-1

		System.out.println("------替换字符串中某字符---------");
		String s18 = "zabjkl";
		String reS18 = s18.replace('a','v');
		System.out.println(reS18);  // zvbjkl
		String s19 = "一支梨花";
		String reS19 = s19.replace("梨花", "桃花");
		System.out.println(reS19);  // 一支桃花

		System.out.println("------子串---------");
		String s20 = "一支梨花压海棠";
		//从下标为4的字符开始截取子串
		s20.substring(4);  //压海棠
		//从下标为2的字符开始截取子串,截到3为止
		s20.substring(2, 4); //梨花

		System.out.println("------比较---------");
		//按字典顺序比较两个字符串
		//参数字符串等于此字符串,返回0
		//此字符串按字典顺序小于参数字符串,返回小于0的值
		//此字符串按字典顺序大于参数字符串,返回大于0的值
		String s21 = "abd";
		String s22 = new String("abc");
		int result = s21.compareTo(s22);
		System.out.println(result);

		System.out.println("------按字典顺序比较两个字符串,忽略大小写---------");
		result = s21.compareToIgnoreCase(s22);
		System.out.println(result);

		System.out.println("------拆分---------");
		String s23 = "asd-dfg-gjh-jkl";
		//将字符串分割为一个字符串数组
		//根据指定字符进行切割
		String[] splitArray = s23.split("-");
		for(String str:splitArray) {
    
    
			System.out.println(str);
		}

		System.out.println("------将其他数据转为字符串---------");
		//将其他数据类型转换为字符串,包括但不仅限于字符数组
		char[] chs = {
    
    'a','c','n'};
		System.out.println(String.valueOf(chs));
		
		System.out.println("-------Java的split函数的基本用法-------");
         String str="good good study, day day up";
	     String[] strarray=str.split(" ");
	     for (int i = 0; i < strarray.length; i++)
	          System.out.println(strarray[i]);
		}

	
}

猜你喜欢

转载自blog.csdn.net/weixin_44531966/article/details/115710528