我想将此括号内的字符串“ VALUES('1','firstname 1','lastname 1','address 1')”拆分为数组,以便像arr [0] =这样检索数组1,arr [1] ='名字1'...等等
我有点知道我需要使用正则表达式拆分,但是我无法想到最好的模式...
提前谢谢!
答案 0 :(得分:2)
类似这样的原因:
public static void main(String[] args) {
//your example
String text2 = "VALUES ('1', 'firstname 1', 'lastname 1', 'address 1')";
//get a text from ( to )
String getMe = text2.substring(text2.indexOf("(")+1 , text2.lastIndexOf(")"));
//replace all ' with empty space and split it by ,
String[] listOfString = getMe.replaceAll("'", "").split(",");
//print it
System.out.println(Arrays.asList(listOfString));
}