zoukankan      html  css  js  c++  java
  • Java String练习题及答案


    1. 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕


    /**
    * 编写程序将 “jdk” 全部变为大写,并输出到屏幕,截取子串”DK” 并输出到屏幕
    */
    public static void main(String[] args) {
    String s = "jdk";
    s = s.toUpperCase();
    System.out.println(s);
    s = s.substring(1);
    System.out.println(s);
    }

    2.编写程序将String类型字符串”test” 变为 “tset”.

    /**
    * 2.编写程序将String类型字符串”test” 变为 “tset”.
    */
    public static void main(String[] args) {
    String s= "test";
    StringBuffer sb = new StringBuffer(s);
    sb.reverse();
    s = sb.toString();
    System.out.println(s);
    }

    3. 写一个方法判断一个字符串是否对称

    /**
    * 3. 写一个方法判断一个字符串是否对称
    */
    public static void main(String[] args) {
    String s = "asdfgasdf";
    StringBuffer sb =new StringBuffer(s);
    String s1 = sb.reverse().toString();
    if(s.equals(s1)){
    System.out.println("字符串对称");
    }else{
    System.out.println("字符串不对称");
    }
    }

    4. String s = "113@ ere qqq yyui"
    请输出所有子串
    113
    ere
    qqq
    yyui

    /**
    * 4. String s = "113@ ere qqq yyui"
    请输出所有子串
    113
    ere
    qqq
    yyui
    */
    public static void main(String[] args) {
    String s = "113@ ere qqq yyui";
    s = s.replaceAll("@", "");
    String [] ss=s.split(" ");
    for (int i = 0; i < ss.length; i++) {
    System.out.println(ss[i]);
    }
    }

    5. 编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
    “To be or not to be",将变成"oT eb ro ton ot eb."。

    /**
    * 5. 编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
    “To be or not to be",将变成"oT eb ro ton ot eb."。
    */
    public static void main(String[] args) {
    String s = "To be or not to be";
    String ss[] = s.split(" ");
    StringBuffer sb2 = new StringBuffer();
    for (int i = 0; i < ss.length; i++) {
    StringBuffer sb = new StringBuffer(ss[i]);
    sb.reverse();
    sb2.append(sb);
    if(i == ss.length-1){
    sb2.append(".");
    }else{
    sb2.append(" ");
    }
    }
    System.out.println(sb2);
    }

    6.String s=”name=zhangsan age=18 classNo=090728”;
    将上面的字符串拆分,结果如下:
    zhangsan 18 090728

    /**
    * 6.String s=”name=zhangsan age=18 classNo=090728”;
    将上面的字符串拆分,结果如下:
    zhangsan 180 90728
    */
    public static void main(String[] args) {
    String s="name=zhangsan age=18 classNo=090728";
    String[] ss = s.split(" ");
    StringBuffer sb =new StringBuffer();
    for (int i = 0; i < ss.length; i++) {
    String[] ss2 =ss[i].split("=");
    sb.append(ss2[1]);
    sb.append(" ");
    }
    System.out.println(sb);
    }

  • 相关阅读:
    混合开发的坑(3) ---关于es6
    混合开发的坑(2) ---pdf展示
    混合开发的坑(1) ---ajax请求
    vue.js
    vue中 import引入组件
    vue中 静态文件引用注意事项
    Oracle 数据库链接
    Oracle中的NVL,NVL2,NULLIF以及COALESCE函数使用
    Merge into 使用
    C# —— IList, ArrayList与List的区别详解
  • 原文地址:https://www.cnblogs.com/panxuejun/p/10102331.html
Copyright © 2011-2022 走看看