ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CSV Parser

2021-01-14 03:01:43  阅读:14  来源: 互联网

标签:String McDonald Parser list str new sb CSV


 

public class Solution {
  public static void main(String[] args) {
    System.out.println(parseCSV(
        "\"Kobe Bryant, 2020\",AAA, McDonald\"\"\"\"s, \"McDonald\"\"s\",\"\",asdf\"\"\"gh&$#^% \"\"sdfhjn,%$#^%\"\"\""));
    System.out.println(parseCSV(
        "\"Kobe Bryant, 2020\",AAA, McDonald\"\"\"\"s, \"McDonald\"\"s\",\"\",asdf\"\"\"gh&$#^% \"\"sdfhjn,%$#^%\"\"\""));
    System.out.println(parseCSV(
        "\"Kobe Bryant, 2020\",AAA, McDonald\"\"\"\"s, \"McDonald\"\"s\",\"\",asdf\"\"\"gh&$#^% \"\"sdfhjn,%$#^%\"\"\""));
  }

  public static String parseCSV(String str) {
    List<String> list = new ArrayList<>();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < str.length(); ++i) {
      char c = str.charAt(i);
      if (c == '"') {
        Object[] ret = consumeCharsWithinQuotes(str, i + 1);
        String withinQuotes = (String) ret[0];
        i = (Integer) ret[1]; //returned position points to the ending quote
        sb.append(withinQuotes);
      } else if (c == ',') {
        list.add(sb.toString());
        sb.setLength(0);
      } else {
        sb.append(c);
      }
    }
    //add the last item to list
    list.add(sb.toString());
    return String.join(" | ", list);
  }

  private static Object[] consumeCharsWithinQuotes(String str, int start) {
    StringBuilder sb = new StringBuilder();
    for (int i = start; i < str.length(); ++i) {
      char c = str.charAt(i);
      if (c == '"') {
        if (i == str.length() - 1) {
          //this is the ending quote
          return new Object[]{sb.toString(), i};
        }
        char nextChar = str.charAt(i + 1);
        if (nextChar == '"') {
          //this is an escaped quote
          sb.append('"');
          ++i;
        } else {
          //this is an internal ending quote
          return new Object[]{sb.toString(), i};
        }
      } else {
        sb.append(c);
      }
    }

    return null;
  }
}

 

标签:String,McDonald,Parser,list,str,new,sb,CSV
来源: https://www.cnblogs.com/neweracoding/p/14275287.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有