您的位置: 网站首页 > 程序开发 > Java程序设计 > 第5章 字符串操作 > 【5.6 练 习 题】

5.6 练 习 题

 

5.6  练  习  题

1.填空题

(1)比较两个字符串内容是否一样可以使用String类提供的        和        两个方法。

(2)String类的indexOf(char ch)方法用于查找字符串中第一个Unicode为ch的字符的位置,如果查找失败,返回        。

(3)String类的substring(int beginIndex)方法用于返回从                的子串。这里beginIndex的取值范围应该在区间        内,如果越界,返回值为空字符串。

(4)StringBuffer类的                方法可以将StringBuffer中的字符串翻转。

(5)匹配我国电话号码格式010-82800027-8007或0873-6220947的正则表达式为                ,其中后面的分机号可有可无。

2.选择题

(1)定义String text= "This library is distributed in the hope that it will be useful ",然后执行text.indexOf ("is")返回的值为         。

A.2                     B.3                     C.13            D.14

(2)下列方法中,那个不是String类拥有的方法         。

A. length()                                         B. charAt()           

C. append(char ch)                              D. indexOf(int ch)

(3)在Sun的JDK         以上版本中,Java自带了支持正则表达式的包。

A.1.3                  B.1.4                  C.1.5           D.1.6

(4)能匹配工商银行账户号码9558 0000 0000 1234567的正则表达式为         ,假设账户号码必须以9558开头,中间可以用空格分开,也可以没有。

A.9558\s+\d{4}\s+\d{4}\s+\d{7}              B.9558\s?\d{4}\s?\d{4}\s?\d{7}

C.9558\d{4}\d{4}\d{7}                    D.9558.\d{4}.\d{4}.\d{7}

(5)能匹配电子邮件的正则表达式为         ,假设电子邮件必须包含“@”符号和“.”符号,其只能含有英文字母、数字和下划线。

A.\S+@\S+.\S+                               B.\s+@\s+.\s+

C.\S+@\S+\.\S+                               D.\S+\@\S+.\S+

3.问答题

(1)比较String类和StringBuffer类的异同,并指出它们适用的场合。

(2)什么是正则表达式?Java中如何使用正则表达式?

(3)字符串比较都有哪些方法?并说出它们的不同点。

4.上机操作题

(1)给出下面程序的运行结果。

public class Example{

  String str=new String("good");

  char[]ch={'a','b','c'};

  public static void main(String args[]){

    Example ex=new Example();

    ex.change(ex.str,ex.ch);

    System.out.print(ex.str+" and ");

    Sytem.out.print(ex.ch);

  }

  public void change(String str,char ch[]){

    str="test ok";

    ch[0]='g';

  }

}

(2)给出下面程序的运行结果。

class STR1 {

    public static void main(String args[]) {

            String s1 = new String();

        String s2 = new String("String 2");

            char chars[] = { 'a',' ','s','t','r','i','n','g' };

        String s3 = new String(chars);

            String s4 = new String(chars,2,6);

         byte bytes[] = { 0,1,2,3,4,5,6,7,8,9 };

         String s5 = new String(bytes,0,1,5);

             String s6 = new String(bytes,10);

        StringBuffer sb = new StringBuffer(s3);

            String s7 = new String(sb);

            System.out.println(s1);

            System.out.println(s2);

        System.out.println(s3);

            System.out.println(s4);

        System.out.println(s5);

            System.out.println(s6);

        System.out.println(s7);

    }

}

(3)给出下面程序的运行结果。

public class Short{

    public static void main(String args[]){

           StringBuffer  s = new  StringBuffer("Hello");

        if((s.length( )>5)&& (s.append("there") . equals("False")));

            System.out.println("value  is  "+s);

    }

}