字符切割split()方法的应用

编写一个程序,先输入一个字符串a(长度不超过20,如果长度超过20则给出提示并结束程序),再输入单独的一个字符b,然后程序会将字符串a当中出现的所有字符b都删掉,从而得到一个新的字符串a2,然后把这个字符串打印出来。

import java.util.Scanner;
public class split {
    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str =sc.next();
                if (str.length() < 20) {
                    String[] arr = str.split(sc.next());//输入要切割的内容
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < arr.length; i++) {
                        if (i == arr.length) {
                            sb.append(arr[i]);
                        } else {
                            sb.append(arr[i]).append("");
                        }
                    }
                    String r = sb.toString();
                    System.out.println(r);
                }else {
                    System.out.println("您输入的字符串长度超过20");
                }
    }
    }

猜你喜欢

转载自blog.csdn.net/qq_62731133/article/details/123474397#comments_21969387