【String】String的一些常用方法

代码
public class String02 {
    /*
     * public String() 空构造
     * public String(byte[] bytes) 把字节数组转换为字符串
     * String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串
     * String(char[] value) 把字符数组转换为字符串
     * String(char[] value, int offset, int count) 把字符数组的部分转换为字符串
     */
    public static void main(String[] args) {
        System.out.println("String的一些常用方法");

        String s1 = new String();
        System.out.println(s1);
        
        byte[] b1 = {97,98,99};
        System.out.println(new String(b1));
        
        byte[] b2 = {97,98,99,100,101,102};
        System.out.println(new String(b2,2,3));
        
        char[] c1 = {'a','b','c','d'};    
        System.out.println(new String(c1));
        
        char[] c2 = {'a','b','c','d','e'};
        System.out.println(new String(c2,3,2));
    }

}
运行结果
String的一些常用方法

abc
cde
abcd
de


代码

public class String04 {

	/**
	 * boolean equals(Object obj) 比较字符串内容是否相等  区分大小写
	 * boolean equalsIgnoreCase(String str) 比较字符串内容是否相同  忽略大小写
	 * boolean contains(String str) 判断一个字符串是否含有另一个字符串
	 * boolean startsWith(String str) 判断字符串是否以某个指定的字符串开头
	 * boolean endsWith(String str)	判断某个字符串是否以某个指定的字符串结尾
	 * boolean isEmpty() 判断字符串是否为空
	 */
	public static void main(String[] args) {
		System.out.println("String的常用方法");
		
		String s1 = "csdn";
		String s2 = "csdn";
		String s3 = "Csdn";
		System.out.println(s1.equals(s2));//true
		System.out.println(s1.equals(s3));//false
		System.out.println(s1.equalsIgnoreCase(s3));//true
		
		String s4 = "This is csdn";
		System.out.println(s4.contains(s2));//true
		
		String s5 = "This";
		System.out.println(s4.startsWith(s5));//true
		System.out.println(s4.endsWith(s2));//true
		
		String s6 = "";
		String s7 = null;
		System.out.println(s6.isEmpty());//true
		System.out.println(s7.isEmpty());//空指针异常
		//null 与 ""区别: 
		// ""是字符串常亮,同时也是一个String类的对象,既然是对象当然也可以调用String类中的方法
		// null 是空常量 不能调用任何的方法,否则会出现空指针异常 null常亮可以给任意的引用数据类型赋值
		
	}

}
代码

public class String02 {

	/**
	 * int length() 获取字符串长度
	 * int indexof(int ch) 返回指定字符在此字符串中第一次出现的索引值
	 * int indexof(String str) 返回字符串在此字符串中第一次出现的索引值
	 * int indexof(int ch,int fromIndex) 返回指定字符 在此字符串中某一位之后第一次出现的位置
	 * int indexof(String str,int fromIndex) 返回指定字符串 在此字符串中某一位之后第一次出现的位置
	 * int lastIndexOf(int ch) 返回指定字符从后往前找第一次出现的索引位置
	 * int lastIndexOf(int ch,int fromIndex) 返回指定字符从指定位置往前找第一次出现的索引位置
	 * String substring(int start) 从指定位置开始截取字符串,默认到末尾
	 * String substring(int start, int end) 街区某一段的字符串
	 * char charAt(int index) 获取指定索引位置的字符
	 */
	public static void main(String[] args) {
		System.out.println("String常用方法使用");
		
		String s1 = "csdn";
		String s2 = "中文测试";
		System.out.println(s1.length());//4
		System.out.println(s2.length());//4
		
		System.out.println(s1.indexOf('s'));//1
		System.out.println(s1.indexOf("sd"));//1
		System.out.println(s1.indexOf("ndsc"));//-1
		
		String s3 = "This is csdn.";
		System.out.println(s3.indexOf('s',5));//6
		System.out.println(s3.indexOf(s1, 3));//8
		System.out.println(s3.lastIndexOf('s'));//9
		
		System.out.println(s3.substring(5));//is csdn.
		System.out.println(s3.substring(2, 6));//is i
		
		for(int i = 0; i < s1.length(); i++) {
			System.out.print(s1.charAt(i));
		}
	}
}

代码

public class String03 {

	/**
	 * 统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数
	 * 分析字符串是由字符组成的,而字符的值都是有范围的,通过范围判断是否包含该字符
	 */
	public static void main(String[] args) {
		String s1 = "ABCDEFabcdef123456!@#$%^&";
		
		int big = 0;
		int small = 0;
		int num = 0;
		int other = 0;
		
		for(int i = 0; i < s1.length(); i++) {
			char c = s1.charAt(i);
			if((c >= 'A') && (c <= 'Z')) {
				big++;
			} else if((c >= 'a') && (c <= 'z')) {
				small++;
			} else if((c >= '0') && (c <= '9')) {
				num++;
			} else {
				other++;
			}
		}
		System.out.println(big);
		System.out.println(small);
		System.out.println(num);
		System.out.println(other);

	}

}

代码

public class String04 {

	/**
	 * byte[] getBytes[] 把字符串转换为字节数组
	 * char[] toCharArray() 把字符串转换为字节数组
	 * static String valueOf(char[] chs) 把字节数组转换为字符串
	 * static String valueof(int ) 把int 类型的数据转换为字符串
	 * 		valueOf可以把任意类型的数据转换为
	 * String toLowerCase() 把字符串转换为小写
	 * String toUpperCase() 把字符串转换为大写
	 */
	public static void main(String[] args) {
		System.out.println("String常用方法使用");

		String s1 = "csdn";
		byte[] arr = s1.getBytes();
		for (int i = 0; i < arr.length; i++) {
			System.out.print(arr[i] + "  ");//99  115  100  110  
		}
		System.out.println();
		
		char[] c = s1.toCharArray();
		for (int i = 0; i < c.length; i++) {
			System.out.print(c[i] + "  ");//c  s  d  n
		}
		System.out.println();
		
		String s2 = String.valueOf(c);
		System.out.println(s2);//csdn
		String s3 = String.valueOf(123456);
		System.out.println(s3);//123456
		
		String s4 = s1.toUpperCase();
		System.out.println(s4);//CSDN
		
		String s5 = s4.toLowerCase();
		System.out.println(s5);//csdn
	}

}

代码

public class String05 {

	public static void main(String[] args) {
		System.out.println("String常用方法使用");
		
		String s1 = "csdn";
		String s2 = "CSDN";
		
		System.out.println(toUpper(s1));
		System.out.println(toUpper(s2));
		
		System.out.println(toUpper2(s1));
		System.out.println(toUpper2(s2));
		
		
	}
	
	static String toUpper2(String str) {
		
		if(str.length() == 0) {
			return null;
		} else {
			str = str.substring(0,1).toUpperCase() + str.substring(1);
		}
		
		return str;
	}
	
	static String toUpper(String str) {
		
		if(str.length() == 0) {
			return null;
		} else {
			char[] b = str.toCharArray();
			if((b[0] >= 'a') && (b[0] <= 'z')) {
				b[0] = (char) (b[0] - 32);
			}
			
			str = new String(b);
		}
		
		return str;
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值