Airbnb的电面题目-2018暑期实习

记一次Airbnb的电面题目:

1.题目描述

将一个.csv文件进行切分。
已知一个.csv文件是按照逗号分割的每个item的,但是如果一个item里面自己有逗号,则整个item都会用双引号扩起来,如果item里面有双引号,则其前面再加一个双引号进行转义。举例如下:
John,Smith,”john”“[email protected]”,”Los,Angeles”,1
应该对应的真正字符串是:
John,Smith,john”[email protected],Los,Angeles,1

输出要求:将各个item用|进行连接,上面的例子输出结果应该为:
John|Smith|john”[email protected]|Los,Angeles|1

2.实现code

    import java.util.ArrayList;

    public class Main {
        public static void main(String[] args) {
            String str="John,Smith,\"john\"\"[email protected]\",\"Los,Angeles\",1";
            helper(str);
        }
         private static void helper(String str){
            ArrayList res=new ArrayList<>();
            int len=str.length();
            StringBuilder sb=new StringBuilder();
            boolean isIn=false;//记录当前是否在一个双引号内
            for(int i=0;i

你可能感兴趣的:(java面试问题)