I want to split the string with semicolon into array of strings, but whenever it is splitting the string with semicolon I need to add that semicolon to the first splitting string. It is adding to the next splitting screen.
我想用分号将字符串拆分为字符串数组,但每当用分号拆分字符串时,我需要将分号添加到第一个拆分字符串。它正在添加到下一个分割屏幕。
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]);
}`
The output that I am expecting is:
我期待的输出是:
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 ;
The output that I am getting is:
我得到的输出是:
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 : ;
2 个解决方案
#1
1
Try to use this regex "(?<=;)",-1
you can incluse ;
:
尝试使用这个正则表达式“(?<=;)”, - 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]);
}
}
Output:
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 ;
#2
1
You can use this regex based on lookbehind:
您可以根据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]);
}
Output:
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 ;
#1
1
Try to use this regex "(?<=;)",-1
you can incluse ;
:
尝试使用这个正则表达式“(?<=;)”, - 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]);
}
}
Output:
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 ;
#2
1
You can use this regex based on lookbehind:
您可以根据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]);
}
Output:
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 ;