JAVA小课堂—————String方法的使用

String方法:

 编译实践:


public class demo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//.length() 字符串的长度
		String a="JAVA";
		System.out.println(a.length());
		System.out.println("=====");
		//.charAt() 截取字符;
		char b=a.charAt(0);//截取第一位
		System.out.println(b);
		System.out.println("=====");
		for (int i = 0; i < a.length(); i++) {
			System.out.println(a.charAt(i));//逐一截取输出
		}
		System.out.println("=====");
		//.getChars() 截取多个字符并由其它字符串接收 
		char[] a1=new char[4];
		a.getChars(0, 2, a1, 1);//getChars(a的开始位,截取a的位数,接收的名,该名的第几位开始)
		System.out.println(a1);
		System.out.println("=====");
		//.equals() .equalsIgnoreCase 比较两个字符串是否相等   结果是Boolean值   前者区分大小写,后者不区分大小写
		String c="JAVA";
		String c1="java";
		System.out.println(c.equals(c1));//区分大小写
		System.out.println(c.equalsIgnoreCase(c1));//不区分大小写
		System.out.println("=====");
		//.toLowercase() 小写字母转换  .toUppercase()  大写字母转换  
		System.out.println(c.toLowerCase().equals(c1.toUpperCase()));
		System.out.println("=====");
		//.concat() 连接字符串  .trim()  去掉开始和结束的空格 
		String d="hello";
		String d1=" hello ";
		System.out.println(d.concat(d1));
		System.out.println(d.concat(d1.trim()));
		System.out.println("=====");
		//.substring() 字符串截取  
		String e="hello";
		System.out.println(e.substring(1));//ello  .substring(开始位到最后) 
		System.out.println(e.substring(1,2));//e  .substring(开始位,到结束位(不包括结束位))
		System.out.println("=====");
		//.indexof(" ") 正着数的位数  .lastIndexof(" ")倒着数的位数
		String e1="hello";
		System.out.println(e1.indexOf("h"));//0
		System.out.println(e1.indexOf("hell"));//0 结果以第一个位数位准
		System.out.println(e1.lastIndexOf("o"));//4
		System.out.println("=====");
		//.split() 返回是一个数组 
		String[] e2=e1.split(" ");
			System.out.println(e2[0]);
	}

}

运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值