题解 | #参数解析#

参数解析

https://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String nextLine = in.nextLine();
            if (Objects.isNull(nextLine) || nextLine.equals("")) {
                break;
            }
            // xcopy /s "C:\\program files" "d:\"
            // 找出所有空格的位置
            List<Integer> spaceIndex = new ArrayList<>();
            String spaceRegex = " ";
            Pattern compile1 = Pattern.compile(spaceRegex);
            Matcher matcher1 = compile1.matcher(nextLine);
            while (matcher1.find()) {
                spaceIndex.add(matcher1.start());
            }
            // 找出所有双引号的位置
            List<Integer> quoteIndex = new ArrayList<>();
            String quoteRegex = "\"";
            Pattern compile2 = Pattern.compile(quoteRegex);
            Matcher matcher2 = compile2.matcher(nextLine);
            while (matcher2.find()) {
                quoteIndex.add(matcher2.start());
            }
            while (!quoteIndex.isEmpty()) {
                Integer quoteIndex1 = quoteIndex.remove(0);
                Integer quoteIndex2 = quoteIndex.remove(0);
                spaceIndex = spaceIndex.stream()
                    .filter(item -> !(item > quoteIndex1 && item < quoteIndex2))
                    .collect(Collectors.toList());
            }
            System.out.println(spaceIndex.size() + 1);
            if (spaceIndex.size() > 1) {
                for (int i = 0; i < spaceIndex.size(); i++) {
                    if (i == 0) {
                        System.out.println(nextLine.substring(0, spaceIndex.get(i))
                            .replace("\"", ""));
                    } else if (i == spaceIndex.size() - 1) {
                        System.out.println(nextLine.substring(spaceIndex.get(i - 1) + 1, spaceIndex.get(i))
                            .replace("\"", ""));
                        System.out.println(nextLine.substring(spaceIndex.get(i) + 1)
                            .replace("\"", ""));
                    } else {
                        System.out.println(nextLine.substring(spaceIndex.get(i - 1) + 1, spaceIndex.get(i))
                            .replace("\"", ""));
                    }
                }
            } else {
                System.out.println(nextLine.substring(0, spaceIndex.get(0))
                    .replace("\"", ""));
                System.out.println(nextLine.substring(spaceIndex.get(0) + 1)
                    .replace("\"", ""));
            }
        }
    }
}

全部评论

相关推荐

迷茫的大四🐶:你这个拿去投央国企吧,投私企包过不了的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务