我想用分号将字符串拆分成字符串数组,但是每当它用分号拆分字符串时,我需要将分号添加到第一个拆分字符串中。它正在添加到下一个分裂屏幕。
String sampleContent="hello ; hai ; come fast ;";
String SQLScripts[] = sampleContent.split("(?=\\;)",-1);
System.out.println(" SQLSCript Length is:"+SQLScripts.length);
for(int m=0;m<SQLScripts.length;m++){
System.out.println("After SQLScripts spliting with semi colon is : "+SQLScripts[m]);
}`我期望的输出是:
SQLSCript Length is:4
After SQLScripts spliting with semi colon is : hello ;
After SQLScripts spliting with semi colon is : hai ;
After SQLScripts spliting with semi colon is : come fast ;我得到的输出是:
SQLSCript Length is:4
After SQLScripts spliting with semi colon is : hello
After SQLScripts spliting with semi colon is : ; hai
After SQLScripts spliting with semi colon is : ; come fast
After SQLScripts spliting with semi colon is : ;发布于 2015-12-16 07:14:28
尝试使用这个正则表达式"(?<=;)",-1,您可以包括;
public static void main(String[] args) {
String sampleContent = "hello ; hai ; come fast ;";
String SQLScripts[] = sampleContent.split("(?<=;)",-1);
System.out.println("SQLSCript Length is:"+SQLScripts.length);
for(int m=0;m<SQLScripts.length-1;m++){
System.out.println("After SQLScripts spliting with semi colon is : "+SQLScripts[m]);
}
}输出:
SQLSCript Length is:4
After SQLScripts spliting with semi colon is : hello ;
After SQLScripts spliting with semi colon is : hai ;
After SQLScripts spliting with semi colon is : come fast ;发布于 2015-12-16 07:11:19
您可以基于lookbehind使用此正则表达式:
String sampleContent="hello ; hai ; come fast ;";
String SQLScripts[] = sampleContent.split("(?<=;)\\s+");
System.out.println(" SQLSCript Length is:"+SQLScripts.length);
for(int i=0;i<SQLScripts.length;i++){
System.out.println("After SQLScripts spliting with semi colon is : "+SQLScripts[i]);
}输出:
SQLSCript Length is:3
After SQLScripts spliting with semi colon is : hello ;
After SQLScripts spliting with semi colon is : hai ;
After SQLScripts spliting with semi colon is : come fast ;https://stackoverflow.com/questions/34305818
复制相似问题