java csv 数组_Java 将CSV列表转换为字符串数组。

这个Java代码示例展示了如何将包含逗号分隔的CSV字符串转换为字符串数组。方法包括使用特定分隔符拆分字符串,并提供了一个删除特定字符的辅助功能。
摘要由CSDN通过智能技术生成

//package com.nowjava;

import java.util.ArrayList;/*nowjava.com - 时 代 Java*/

import java.util.Collection;

import java.util.Collections;

import java.util.Enumeration;

import java.util.List;

public class Main {

public static void main(String[] argv) throws Exception {

String str = "nowjava.com";

System.out.println(java.util.Arrays

.toString(commaDelimitedListToStringArray(str)));

}

/**

* Convert a CSV list into an array of Strings.

*

* @param str

* the input String

* @return an array of Strings, or the empty array in case of empty input

*/

public static String[] commaDelimitedListToStringArray(final String str) {

/*

来自

*NowJava.com - 时代Java*/

return delimitedListToStringArray(str, ",");

}

/**

* Take a String which is a delimited list and convert it to a String array.

A single delimiter can

* consists of more than one character: It will still be considered as single delimiter string, rather

* than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

*

* @param str

* the input String

* @param delimiter

* the delimiter between elements (this is a single delimiter, rather than a bunch individual

* delimiter characters)

* @return an array of the tokens in the list

* @see #tokenizeToStringArray

*/

public static String[] delimitedListToStringArray(final String str,

final String delimiter) {

return delimitedListToStringArray(str, delimiter, null);

}

/**

* Take a String which is a delimited list and convert it to a String array.

A single delimiter can

* consists of more than one character: It will still be considered as single delimiter string, rather

* than as bunch of potential delimiter characters - in contrast to tokenizeToStringArray.

*

* @param str

* the input String

* @param delimiter

* the delimiter between elements (this is a single delimiter, rather than a bunch individual

* delimiter characters)

* @param charsToDelete

* a set of characters to delete. Useful for deleting unwanted line breaks: e.g. "\r\n\f" will

* delete all new lines and line feeds in a String.

* @return an array of the tokens in the list

* @see #tokenizeToStringArray

*/

public static String[] delimitedListToStringArray(final String str,

final String delimiter, final String charsToDelete) {

if (str == null)

return new String[0];

if (delimiter == null)

return new String[] { str };

final Listresult = new ArrayList();

if ("".equals(delimiter)) {

for (int i = 0; i < str.length(); i++) {

result.add(deleteAny(str.substring(i, i + 1), charsToDelete));

}

} else {

int pos = 0;

int delPos;

while ((delPos = str.indexOf(delimiter, pos)) != -1) {

result.add(deleteAny(str.substring(pos, delPos),

charsToDelete));

pos = delPos + delimiter.length();

}

if ((str.length() > 0) && (pos <= str.length())) {

// Add rest of String, but not in case of empty input.

result.add(deleteAny(str.substring(pos), charsToDelete));

}

}

return toStringArray(result);

}

/**

* Delete any character in a given String.

*

* @param inString

* the original String

* @param charsToDelete

* a set of characters to delete. E.g. "az\n" will delete 'a's, 'z's and new lines.

* @return the resulting String

*/

public static String deleteAny(final String inString,

final String charsToDelete) {

if (!hasLength(inString) || !hasLength(charsToDelete))

return inString;

final StringBuilder sb = new StringBuilder();

for (int i = 0; i < inString.length(); i++) {

final char c = inString.charAt(i);

if (charsToDelete.indexOf(c) == -1) {

sb.append(c);

}

}

return sb.toString();

}

/**

* Copy the given Collection into a String array. The Collection must contain String elements only.

*

* @param collection

* the Collection to copy

* @return the String array (null if the passed-in Collection was null)

*/

public static String[] toStringArray(final Collectioncollection) {

if (collection == null)

return null;

return collection.toArray(new String[collection.size()]);

}

/**

* Copy the given Enumeration into a String array. The Enumeration must contain String elements only.

*

* @param enumeration

* the Enumeration to copy

* @return the String array (null if the passed-in Enumeration was null)

*/

public static String[] toStringArray(

final Enumerationenumeration) {

if (enumeration == null)

return null;

final Listlist = Collections.list(enumeration);

return list.toArray(new String[list.size()]);

}

/**

* Check that the given CharSequence is neither null nor of length 0. Note: Will return

/**代码未完, 请加载全部代码(NowJava.com).**/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值