第五章 字符串

1、大小写字母转换

/**
 * 大小写字母转换
 * @author DELL
 *
 */
public class One {
  public static void main(String[] args) {
	String str=new String("abc DEFGH");
	String newstr1=str.toLowerCase();
	String newstr2=str.toUpperCase();
	System.out.println(newstr1);
	System.out.println(newstr2);
}
}

结果:

 2、分别截取字符串str1和字符串str2中的部分内容,如果截取后的两个子串相同(不区分大小写)会输出“两个子串相同”,否则输出“两个子串不相同”

/**
 * 截取子字符串,比较大小
 * @author DELL
 *
 */
public class Two {
public static void main(String[] args) {
	String str1="我是张琪";
	String str2="张琪是张嘉仪的friend";
	String substr1=str1.substring(2);
	String substr2=str2.substring(0,2);
	if(substr1.equals(substr2)){
		System.out.println(substr1+substr2+"两个子串相同");
	}else{
		System.out.println(substr1+substr2+"两个子串不相同");
	}
			
}
	
}

结果:

 3、使用正则表达式来判断字符串是否为合法手机号


import java.util.Scanner;

/**
 * 验证大陆手机号
 * @author DELL
 *
 */
public class Zhengze {
   public static void main(String[] args) {
	Scanner sc= new Scanner(System.in);
	String regex="(13[0-9]|15[0-9&&[^4]]|18[056789])\\d{8}";
	while(true){//循环读取
		String phone=sc.nextLine();
		if(phone.matches(regex)){
			System.out.println("您输入的是手机号");
			
		}else{
			System.out.println("您输入了不合法的手机号");
		}
	}
}
}

结果:

4、使用字符串生成器,将字符串str追加1~10这10个数字

**
 * 字符串追加内容
 * @author DELL
 *
 */
public class Builder {
public static void main(String[] args) {
	StringBuilder builder=new StringBuilder("");
	for(int i=1;i<=10;i++){
		builder.append(i);
	}
	System.out.println(builder);
}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值