当前位置: 移动技术网 > IT编程>开发语言>Java > java实现CSV 字段分割

java实现CSV 字段分割

2019年07月22日  | 移动技术网IT编程  | 我要评论

支持引号嵌套,逗号分割

private static string[] cvsfield(string line){
    list<string> fields = new linkedlist<>();
    char[] alpah = line.tochararray();
    boolean isfieldstart = true;
    int pos = 0; int len = 0; boolean yinhao = false;
    for(char c : alpah){
      if(isfieldstart){
        len = 0;
        isfieldstart = false;
      }
      if(c == '\"'){
        yinhao = !yinhao;
      }
      if(c == ',' && !yinhao){
        fields.add(new string(alpah, pos - len, len));
        isfieldstart = true;
      }
      pos++; len++;
    }
    return fields.toarray(new string[0]);
  }

以上所述就是本文的全部内容了,希望大家能够喜欢。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网