• java作业4


    (一)  请查看String.equals()方法的实现代码,注意学习其实现方法。(发表到博客作业上)

     

    (二)  整理String类的Length()charAt() getChars()replace() toUpperCase() toLowerCase()trim()toCharArray()使用说明

    Length():获取字串长度

        String s1 = "Welcome to java";

        System.out.println("s1's length is: " + s1.length());

    运行结果:

    s1's length is: 15

             charAt():获取指定位置的字符

    String s1 = "Welcome to java";

    System.out.println(s1.substring(4));

    System.out.println(s1.substring(2, 7));

    运行结果:

    ome to java

    lcome

    getChars():获取从指定位置起的子串复制到字符数组中(它有四个参数,在示例中有介绍)

    char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!

    String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!

    System.out.println( s1);

    运行结果:I love you!

    replace():子串替换

    String s = "Welcome to java";

    System.out.println(s.replace('e', 'm'));

    System.out.println(s.replaceFirst("e", "AB"));

    System.out.println("eleleledsafsdfhie".replaceAll("el", "wml"));

    运行结果:

    Wmlcomm to java
    WABlcome to java
    wmlwmlwmledsafsdfhie

     

    toUpperCase() toLowerCase():大小写转换

    String s = "Welcome to java";

    System.out.println("s.toUpperCase():" +s.toUpperCase()); //s.toUpperCase(): WELCOME TO JAVA

    System.out.println("s.toLowerCase(): " + s.toLowerCase()); //s.toLowerCase(): welcome to java

    trim():去除头尾空格:

    String s = "  weer ewre ";

    System.out.println(s.trim());  // weer ewre

    toCharArray():将字符串对象转换为字符数组

    char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!

    String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!

    System.out.println( s1 );

    运行结果:

    I love you!

    (三)字串加密

    设计思想: 用charAt()函数获取字符串中的每个字符,加密时所有字符改成本字符在字母表中后三位位置的字符,但XYZ变成ABC,解密时所有字符改成本字符在字母表中前三位位置的字符,但ABC变成XYZ。

     

     

                                

      源代码:

     

     

     

     

     结果截图:

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Python开发基础--- IO模型
    进程队列补充-创建进程队列的另一个类JoinableQueue
    Python开发基础--- Event对象、队列和多进程基础
    Python开发基础---多线程锁机制
    Python开发基础----多线程
    Python开发基础----socket套接字基础2
    Python开发基础----异常处理、socket套接字基础1
    Python开发基础----反射、面向对象进阶
    Python开发基础---多态与多态性、绑定方法和非绑定方法
    ubuntu添加新用户
  • 原文地址:https://www.cnblogs.com/jinpeigang/p/4905653.html
Copyright © 2020-2023  润新知