Java教程

《Java从入门到精通》第5章 字符串 习题

本文主要是介绍《Java从入门到精通》第5章 字符串 习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.使用String类的toUpperCase()方法和toLowerCase()方法来实现大小写的转换

	String str =new String("abc DEF");
		String newstr=str.toUpperCase();
		String newstr1=str.toLowerCase();
		System.out.println("大写字母"+newstr);
		System.out.println("小写字母"+newstr1);
		System.out.println("-----------------------");

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

public static void main(String[] args) {
 /*
 * 分别截取字符串str1和字符串str2中的部分内容。如果截取后的两个子串相同(不区分大小写)输出相同,否则输出不同
 * substring(beginIndex,endIndex)
 * 下标从0开始,空格占用一个索引位置
 * 比较equals 区分大小写的。
 */
	String st1=new String("hello1word");
	String st2=new String("Hello2beijing");
	String substr1=st1.substring(0,5);
	String substr2=st2.substring(0,5);
	System.out.println(substr1);
	System.out.println(substr2);
	boolean b =substr1.equals(substr2);//equals区分大小写
	System.out.println("equals区分大小写的真假判断两字符:"+b);
	boolean a=substr1.equalsIgnoreCase(substr2);//equalsIgnoreCase忽略大小写
	if (b==false) {
		System.out.println("equals区分大小写的判断两字符相等结果:两字符不相等");
	}else {
		System.out.println("equals区分大小写的判断两字符相等结果:字符相等");
	}
	System.out.println();
	
	System.out.println("equalsIgnoreCase忽略大小写判断两字符相等:"+a);
	if (a==false) {
		System.out.println("equalsIgnoreCase忽略大小写的判断两字符相等结果:两字符不相等");
	}else {
		System.out.println("equalsIgnoreCase忽略大小写的判断两字符相等结果:字符相等");
	}
}

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

package char5;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MobileNum {
	public static boolean isMobileNu(String mobiles){		
		/*
		 * Pattern.compile 预编译正则表达式
		 * \\D 代表任何一个非数字字符
		 * \\d表示0~9中的任何一个
		 * ^ 和 $ 他们是分别用来匹配字符串的开始和结束
		 * matches(regex) 方法用于检测字符串是否匹配给定的正则表达式。regex -- 匹配字符串的正则表达式。
		 */				
	Pattern p =Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9])|(19[0-9]))\\d{8}$");
	Matcher m=p.matcher(mobiles);//mobiles是否满足p的预编译规则,满足则返回m的值为TRUE
	//System.out.println(mobiles+m.matches());
	boolean b=m.matches();
	if (b==false) {
		System.out.println(mobiles+"不是手机号");
	}else {
		System.out.println(mobiles+"是手机号");
	}
	return m.matches();			
	}
	public static void main(String[] args) {
		String str2=new String("13456478688");
		MobileNum.isMobileNu(str2);	
		String str1=new String("13004567890");
		String guize="^((13[0-9])|(15[^4,\\D])|(18[0,5-9])|(19[0-9]))\\d{8}$";	
 		if (str1.matches(guize)) {
 			System.out.println(str1+"是电话号码");
 			}else {
 				System.out.println(str1+"不是电话号码");
 			}
	}
	
}
public static void main(String[] args) {
     String str1=new String("13004567890");
		String guize="^((13[0-9])|(15[^4,\\D])|(18[0,5-9])|(19[0-9]))\\d{8}$";	
     		if (str1.matches(guize)) {
     			System.out.println(str1+"是电话号码");
     			}else {
     				System.out.println(str1+"不是电话号码");
     			}
  
	}

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

package char5;
public class StringInsert {
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		StringBuilder bf1=new StringBuilder("hello");
		StringBuilder bf=new StringBuilder("helloQ");
		/*append(content)  content表示要追加到字符串生成器中的内容,可以是任何类型的数据或者其他对象
		 * insert(int offset,arg) offset:字符串生成器的位置。arg:将要插入的位置,参数可以是任何穗局类型或对象
		 *insert 的插入是倒序插入
		 */
		 for(int i=1;i<=10;i++) {
			 bf.insert(5, i);
			 System.out.print(i+"_");
		 }
		 System.out.println();
		 //bf.insert(5, "1234567810");
		 System.out.println(bf.toString());				 
		 for(int i=1;i<=10;i++) {
			 bf1.append(i+" ");
		 }
		 System.out.println(bf1);
	}
}

这篇关于《Java从入门到精通》第5章 字符串 习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!