zoukankan      html  css  js  c++  java
  • Java 【String方法的使用】(了解75%~80%)

    第一题:
    定义一个字符串s = "Hello-World",利用API完成如下小需求
    1.判断字符串s,与字符串"World"是否相等,并打印出来.
    2.用程序得到字符串"Wo",在字符串s中的起始索引.
    3.得到s中,3号索引对应的字符,打印到控制台上
    4.得到s的长度,打印在控制台上.
    5.获得s中的"Hell"字符串,打印在控制台上.
    6.获得s中的"orld"字符串,打印在控制台上.
    7.将字符串s中的所有o替换为*号.打印在控制台上
    8.将字符串s切割成"Hello"和"World"两个字符串,打印在控制台上
    9.将字符串s变为字符数组,遍历数组将每个字符打印在控制台上

    public class Demo01 {
        public static void main(String[] args) {
            String s = "Hello-World";
            //1.判断字符串s,与字符串"World"是否相等,并打印出来.
            if (s=="World"){
                System.out.println(true);
            }else {
                System.out.println(false);
            }
            //2.用程序得到字符串"Wo",在字符串s中的起始索引.
            CharSequence i = s.subSequence(6,8);
            System.out.println(i);
            //3.得到s中,3号索引对应的字符,打印到控制台上
            char a = s.charAt(3);
            System.out.println(a);
            //4.得到s的长度,打印在控制台上.
            int b = s.length();
            System.out.println(b);
            //5.获得s中的"Hell"字符串,打印在控制台上.
            String c = s.substring(0, 4);
            System.out.println(c);
            //6.获得s中的"orld"字符串,打印在控制台上.
            String d = s.substring(7, 11);
            System.out.println(d);
            //7.将字符串s中的所有o替换为*号.打印在控制台上
            String e = s.replace("o", "*");
            System.out.println(e);
            //8.将字符串s切割成"Hello"和"World"两个字符串,打印在控制台上
            String[] s1 = s.split("-|#");
            System.out.println("s1的数组长度为:"+s1.length);
            for (int l = 0; l < s1.length; l++) {
                System.out.println(s1[l]);
            }
            //9.将字符串s变为字符数组,遍历数组将每个字符打印在控制台上
            Map<Character,Integer> map=new HashMap<>();
            char[] c1 = s.toCharArray();
            for (char w:c1) {
                Integer q=map.get(w);
                if (q==null){
                    map.put(w,1);
                }else{
                    map.put(w,q.intValue()+1);
                }
            }
            System.out.println(map);
        }
    }

    第二题:
    1.键盘录入一个字符串
    2.统计录入的字符串中的大写字母,小写字母,数字分别有多少个.

    public class Demo02 {
        public static void main(String[] args) {
            String a="I Love You";
            //定义三个统计变量
            int BigCount=0;
            int smallCount=0;
            int number=0;
            //遍历字符串,得到每一个字符
            for (int i = 0; i <a.length() ; i++) {
                char c = a.charAt(i);
            //判断该字符是那种类型
            if (c>='a'&&c<='z'){
                smallCount++;
            }else if (c>='A'&&c<='Z'){
                BigCount++;
            }else if (c>='0'&&c<='9'){
                number++;
            }
            }
            //打印输出
            System.out.println("大写字母:"+BigCount+"个");
            System.out.println("小写字母:"+smallCount+"个");
            System.out.println("数字:"+number+"个");
        }
    }    

    第三题:
    1.键盘录入5个字符串,组成一个数组
    2.统计录入的字符串数组中的大写字母,小写字母,数字分别有多少个.

    public class Demo03 {
        public static  void main(String[] args) {
                    //String s = "I Love you";
                    Scanner s1=new Scanner(System.in);
                    System.out.println("请输入五个字符串:");
                    String s = s1.next();
                    char[] chars = s.toCharArray();
                    int BigCount=0;
                    int smallCount=0;
                    int number=0;
                    for (int i = 0; i <chars.length ; i++) {
                        if ('a'<=chars[i]&&chars[i]<='z'){
                            BigCount++;
                        }
                        if ('A'<=chars[i]&&chars[i]<='Z'){
                            smallCount++;
                        }
                        if ('0'<=chars[i]&&chars[i]<='9'){
                            number++;
                        }
                    }
                    System.out.println("大写字母:"+BigCount+"个");
                    System.out.println("小写字母:"+smallCount+"个");
                    System.out.println("数字:"+number+"个");
    
                }
            }

    第四题:
    1.键盘录入一个字符串
    2.将该字符串变成字符数组
    3.将字符数组中的所有大写字母变成小写字母
    4.如果第一位和最后一位的内容不相同,则交换
    5.将字符数组中索引为偶数的元素变成'~'
    6.打印数组元素的内容
    ------------------------------
    【结果展示】
    请输入字符串
    abcDEf719
    最终显示的效果
    ~b~d~f~1~

    public class Demo04 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            // 1.键盘录入一个字符串
            String str = scanner.nextLine();
            char[] chars = new char[str.length()];
            // 2.将该字符串变成字符数组(不能使用toCharArray()方法)
            for (int i = 0; i < str.length(); i++) {
                char ch = str.charAt(i);
                // 5.将字符数组中索引为奇数的元素变成'~'
                ch = (i % 2== 0) ? '~' : ch;
                // 3.将字符数组中的所有大写字母变成小写字母(不能使用toLowerCase()方法)
                ch = (ch >= 'A' && ch <= 'Z') ? (char)(ch - 32) : ch;
                chars[i] = ch;
            }
            // 4.如果第一位和最后一位的内容不相同,则交换
            if (chars[0] != chars[chars.length-1]) {
                char ch = chars[0];
                chars[0] = chars[chars.length - 1];
                chars[chars.length-1] = ch;
            }
            // 6.打印数组元素的内容
            System.out.println(Arrays.toString(chars));
        }
    }            

    第五题:
    1.键盘录入一个字符串
    2.从字符串中随机获取3次字符,将获取的3个字符组成一个新的字符串.打印到控制台上

    public class Demo05 {
        public static void main(String[] args) {
            String str="I Love you";
            for (int i = 0; i <str.length(); i++) {
                String s = str.substring(i, i + 1);
                if((str.contains(s + s + s)))
                    //String s1 = s.toString();
                    System.out.println(s);
                }
            }
        }

    第六题:
    1.创建一个集合,往集合中键盘录入5个字符串
    2.遍历集合,将集合中长度大于4的元素末尾加上一个X,
    3.遍历集合,将集合打印在控制台上.
    例:键盘录入后的集合{"123","ASDFQ","qq","poiuy","asd"}
    打印到控制台上的集合{"123","ASDFQX","qq","poiuyX","asd"}

    public class Demo06 {
        public static void main(String[] args) {
            Scanner s1=new Scanner(System.in);
            System.out.println("请输入5个字符串:");
    
            String[] s=new String[5];
            for (int i = 0; i <s.length ; i++) {
                String s2 = s.toString();
                String next = s1.next();
                if (s.length>4){
                    System.out.println(next.toString()+"X");
                }
            }
        }
    }

    第七题:
    分析以下需求,并用代码实现
    1.定义如下方法public static String getPropertyGetMethodName(String property)
    功能描述:
    (1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的get方法的名字
    (2)如:用户调用此方法时传入参数为"name",该方法的返回值为"getName"
    传入参数为"age",该方法的返回值为"getAge"

    2.定义如下方法public static String getPropertySetMethodName(String property)
    功能描述:
    (1)该方法的参数为String类型,表示用户传入的参数,返回值类型为String类型,返回值为对应的set方法的名字
    (2)如:用户调用此方法时传入参数为"name",该方法的返回值为"setName"
    传入参数为"age",该方法的返回值为"setAge"

    public class Demo07 {
        public static void main(String[] args) {
                System.out.println(getPropertyGetMethodName("xiaozhu"));
                System.out.println(getPropertySetMethodName("xiaozhu"));
            }
            public static String getPropertyGetMethodName(String property) {
                return "get"+property.substring(0, 1).toUpperCase()+property.substring(1);
    
            }
            public static String getPropertySetMethodName(String property) {
            return "set"+property.substring(0, 1).toUpperCase()+property.substring(1);
    
            }
        }

    第八题:
    完成下列题目要求:
    ①定义方法filter
    要求如下:
    参数:String [] arr,String str
    返回值类型:String []
    实现:遍历arr,将数组中包含参数str的元素存入另一个String 数组中并返回
    PS:返回的数组长度需要用代码获取
    ②在main方法中完成以下要求:
    定义一个String数组arr,数组元素有:"itcast","itheima","baitdu","weixin","zhifubao"
    调用1中的filter方法传入arr数组和字符串”it”,输出返回的String数组中所有元素
    示例如下:
    输出的数组中的元素:
    "itcast","itheima","baitdu"

    public class Demo08 {
        public static class ArrayListExam02 {
            public static void main(String[] args) {
                String[] arr = {"itcast", "zhifubao", "itheima", "weixin", "baitdu"};
                String[] its = filter(arr, "it");
                System.out.println(Arrays.toString(its));
            }
    
            public static String[] filter(String[] arr, String str) {
                ArrayList<String> list = new ArrayList<>();
                for (String s : arr) {
                    if (s.contains(str)) {
                        list.add(s);
                    }
                }
                System.out.println(list);
    
                String[] newArr = new String[list.size()];
                newArr = list.toArray(newArr);
                return newArr;
            }
        }
    }    

    第九题:
    a.定义方法public static ArrayList<String> handleString(String [] arr,String str);
    实现以下功能:
    遍历arr,将数组中包含参数str的元素,含有str的部分替换为*, 存入另一个新String 集合中,将新集合返回;
    b.在main方法中完成以下要求:
    1)定义一个String数组arr,数组元素有:"beijing", "shanghai", "tianjin", "chongqing";
    2)调用handleString方法传入arr数组和字符串”a”,输出返回的String集合中所有元素;

    示例如下:
    控制台输出元素如下:
    [sh*ngh*i,ti*njin]

    第十题:
    1.定义一个工具类MathUtils,包含一个静态方法add,功能是:求两个数之和,并将其返回.
    2.在测试类中的主方法中测试自己定义的工具类,能通过 类名.方法 调用add方法,计算两个数的和

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    另:①不会的就是不会,虚心求教就是了,不然搞得每天都休息不好。

      ②只有休息好,才能学的深入,睡觉!!!

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    感谢到访!期待您的下次光临!

  • 相关阅读:
    温故而知新 js 点击空白处关闭气泡
    javascript 打印错误信息 catch err
    ajax application/json 的坑
    nodejs 的好基友:pm2
    有道翻译 / 百度翻译Api
    PHP 正则表达式
    php 正则替换
    github get 请求指定页面的代码
    H5 input 聚焦 置顶
    autoHotKey 一些脚本积累
  • 原文地址:https://www.cnblogs.com/varchar-pig/p/13843979.html
Copyright © 2011-2022 走看看