如何按字符拆分字符串(Java)
How to split string by character with exceptions (Java)
我想处理 CSV 文件。
但是处理起来有一定的问题:
我需要拆分每一行;
通常我会使用 .split 方法 btu 在这种情况下有例外:
一行可以有空 "cells" 所以它看起来像 x;y;z;;a;;;b
我仍然需要在数组中获取空的。
例如
array[0] = "x";
array[1] = "y";
array[2] = "z";
array[3] = "";
等等。
另一个例外是:
有一个单元格包含 html 代码(其中包含“;”)。
因此,如果 ;在。。。之间 ””。
有办法处理吗?
解析这种形式的数据是一个常见的问题,已经被CSV解析器解决了。您可以使用 Apache Commons CSV 并将分隔符更改为 ;
而不是默认的 ,
.
您可以尝试使用 api OpenCSV。这是做同样事情的小例子,
public class OpenCSVExample {
public static void main(String[] args)
{
CSVReader reader = null;
try
{
//Get the CSVReader instance with specifying the delimiter to be used
reader = new CSVReader(new FileReader("SampleCSVFile.csv"),';');
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上示例取自此参考资料 ParseCSVFiles,请查看以了解更多详细信息。
您可以使用 reg exp 获得它。
public void regExpSeparateWithSemicolon() {
Scanner scanner = null;
String[] result = null;
String testString = null;
String regularExpression = "(?!=\",\");";
int counter = 0;
try {
scanner = new Scanner(
new File("/home/domain/immo/Documents/SampleCsv.csv"));//Path to csv file
while(scanner.hasNext()) {
//String testString = "x;y;z;;a;\";\";b";
testString = scanner.next();
testString = testString.replaceAll("\";\"","\",\"");
result = testString.split(regularExpression);
for(int index = 0; index < result.length; index++) {
System.out.println("result["+counter+++"] = "+
result[index].replace(",", ";"));
}
}
} catch (FileNotFoundException fnf) {
System.out.println("Exception occured :"+fnf);
} catch (Exception e) {
System.out.println("Exception occured :"+e);
} finally {
if(null != scanner) {
scanner.close();
}
}
}
SampleCsv.csv
x;y;z;;a;;;";";b
1;2;3;;4;;;";";5
O/P
result[0] = x
result[1] = y
result[2] = z
result[3] =
result[4] = a
result[5] =
result[6] =
result[7] = ";"
result[8] = b
result[9] = 1
result[10] = 2
result[11] = 3
result[12] =
result[13] = 4
result[14] =
result[15] =
result[16] = ";"
result[17] = 5
I can't figure out how to make it work without the replaceAll.
希望有人能找到。
我想处理 CSV 文件。 但是处理起来有一定的问题:
我需要拆分每一行; 通常我会使用 .split 方法 btu 在这种情况下有例外:
一行可以有空 "cells" 所以它看起来像 x;y;z;;a;;;b
我仍然需要在数组中获取空的。
例如
array[0] = "x";
array[1] = "y";
array[2] = "z";
array[3] = "";
等等。 另一个例外是: 有一个单元格包含 html 代码(其中包含“;”)。 因此,如果 ;在。。。之间 ””。 有办法处理吗?
解析这种形式的数据是一个常见的问题,已经被CSV解析器解决了。您可以使用 Apache Commons CSV 并将分隔符更改为 ;
而不是默认的 ,
.
您可以尝试使用 api OpenCSV。这是做同样事情的小例子,
public class OpenCSVExample {
public static void main(String[] args)
{
CSVReader reader = null;
try
{
//Get the CSVReader instance with specifying the delimiter to be used
reader = new CSVReader(new FileReader("SampleCSVFile.csv"),';');
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上示例取自此参考资料 ParseCSVFiles,请查看以了解更多详细信息。
您可以使用 reg exp 获得它。
public void regExpSeparateWithSemicolon() {
Scanner scanner = null;
String[] result = null;
String testString = null;
String regularExpression = "(?!=\",\");";
int counter = 0;
try {
scanner = new Scanner(
new File("/home/domain/immo/Documents/SampleCsv.csv"));//Path to csv file
while(scanner.hasNext()) {
//String testString = "x;y;z;;a;\";\";b";
testString = scanner.next();
testString = testString.replaceAll("\";\"","\",\"");
result = testString.split(regularExpression);
for(int index = 0; index < result.length; index++) {
System.out.println("result["+counter+++"] = "+
result[index].replace(",", ";"));
}
}
} catch (FileNotFoundException fnf) {
System.out.println("Exception occured :"+fnf);
} catch (Exception e) {
System.out.println("Exception occured :"+e);
} finally {
if(null != scanner) {
scanner.close();
}
}
}
SampleCsv.csv
x;y;z;;a;;;";";b
1;2;3;;4;;;";";5
O/P
result[0] = x
result[1] = y
result[2] = z
result[3] =
result[4] = a
result[5] =
result[6] =
result[7] = ";"
result[8] = b
result[9] = 1
result[10] = 2
result[11] = 3
result[12] =
result[13] = 4
result[14] =
result[15] =
result[16] = ";"
result[17] = 5
I can't figure out how to make it work without the replaceAll.
希望有人能找到。