题解 | #参数解析#

参数解析

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

强行字符串处理就完事了
import java.io.IOException;
import java.util.*;
public class Main{
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        ArrayList<String> list = new ArrayList<>();
        while (str.indexOf(" ") != -1){
            if(str.indexOf("\"") == 0){
                str = str.substring(1);
                String token = str.substring(0, str.indexOf("\""));
                list.add(token);
                str = str.substring(str.indexOf("\"") + 1);
            } else {
                String token = str.substring(0, str.indexOf(" "));
                list.add(token);
                str = str.substring(str.indexOf(" ") + 1);
            }
            if(str.indexOf(" ") == 0){
                str = str.trim();
            }
        }
        if(str.length() > 0){
            if(str.indexOf("\"") != -1){
                list.add(str.substring(1, str.lastIndexOf("\"")));
            } else {
                list.add(str);
            }
        }
        System.out.println(list.size());
        for (String s : list) {
            System.out.println(s);
        }
    }
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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