JavaSE习题 第六章 字符串和正则表达式

Make efforts eveyday

问答题

1.对于字符串

  String s1=new String("ok");

  String s2=new String("ok");

写出下列表达式的值

  s1==s2  false

  s1.equals(s2)   true

2.对于字符串

  String s1=new String("I love you zhht");

  String s2=s1.replaceAll("love","hate");

写出System.out.printf("%s,%s",s1,s2)的结果

3.String类和StringBuffer类有何不同

  String类不可修改字符,StringBuffer类可以修改

4.对于StringBuffer字符串

  StringBuffer str-new StringBuffer("abcdefg");

  str=str.delete(2,4);

写出输出str结果

  abefg

5.StringTokenizer类的主要用途是什么?该类有哪些重要方法?

  分割字符串

  重要方法hasMoreTokens判断是否有下一个迭代对象

  nextToken返回String类型对象

6.下列System.out.printf输出结果是什么

String s=new String("we,go,to,school");

StringTokenizer token=new StringTokenizer(s,",");

String word=token.nextToken();

int n=token.countTokens();

System.out.printf("%s,%d",word,n);

we,3

7.Matcher对象的find()方法和lookingAt()方法有什么不同?

lookingAt是从开头匹配

find是从start位置匹配

8.正则表达式[123]代表什么意思?

123中任意一个

9.写出与模式“A[135]{2}”匹配的四个字符串

A11 A13 A15 A35

10.邪猎哪些字符匹配“boy\\w{3}”

A.boy111  B.boy!@#  C.boyweo  D.boyboyboyboy

C

作业题

1.编写一个应用程序,用户从键盘输入一行字符串,程序输出该字符串中与模式“[24680]A[13579]{2}”匹配的字符串

public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String str=sc.nextLine();
        Pattern p=Pattern.compile("[24680]A[13579]{2}");
        Matcher m=p.matcher(str);
        while(m.find()) {
            System.out.println(m.group());
        }
    }

截图:

 

2.编写一个应用程序,用户从键盘输出一行含有数字的字符串,程序仅仅输出字符串的全部数字字符

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher(str);
while(m.find()) {
System.out.println(m.group());
}
}

截图:

 

posted @ 2018-11-02 16:40  SteveYu  阅读(1216)  评论(0编辑  收藏  举报