我有一个像这样的字符串 [ “1505213753057”, “1505213854042”, “1505537148455”]
我想将其拆分并将值存储到Long Array中。
String [] loanId = loanIds.split("\"?,(?=(?:(?:[^\"]*\"){2})*[^\"]*$)\"?");
Long[] data = new Long[loanId.length];
for (int i = 0; i < loanId.length; i++) {
data[i] = Long.parseLong(loanId[i]);
}
我正在使用这个分离器,但它不起作用。任何帮助如何实现这一目标?
答案 0 :(得分:1)
这应该有效
String loanIds = "[\"1505213753057\",\"1505213854042\",\"1505537148455\"]";
loanIds = loanIds.replaceAll("(\\[\")|(\"\\])", "").replaceAll("\",\"", ",");
String [] loanId = loanIds.split(",");
Long[] data = new Long[loanId.length];
for (int i = 0; i < loanId.length; i++) {
data[i] = Long.parseLong(loanId[i]);
System.out.println(data[i]);
}
答案 1 :(得分:0)
将[
和]
替换为{
和}
。
import java.util.*;
class check {
public static void main(String ar[]){
String str[] ={"1505213753057","1505213854042","1505537148455"};
long ll[]=new long[str.length];
for(int i=0;i<str.length;i++)
ll[i]=Long.parseLong(str[i]);
for(int i=0;i<str.length;i++)
System.out.println(ll[i]);
}
}
答案 2 :(得分:0)
这可以使用正则表达式实现,如下所示:
package com.grsdev.stackoverflow.question170920.pack01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LongNumerString {
public static void main(String[] args) {
String text="[\"1505213753057\",\"1505213854042\",\"1505537148455\"]";
Pattern pattern=Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(text);
List<Long> list=new ArrayList<>();
while(matcher.find()){
list.add(Long.valueOf(matcher.group()));
}
System.out.println("longs : "+list);
Long [] array = new Long[list.size()];
list.toArray(array);
}
}
答案 3 :(得分:0)
mainstring = [&#34; 1505213753057&#34;&#34; 1505213854042&#34;&#34; 1505537148455&#34;] 这是你的字符串吗?
如果是,并且您想要将所有元素存储在长数组中,那么这个myt就是解决方案
String stringWithoutBraces = mainstring.substring(1,mainstring.length() - 1);
List<Long> loanidlist=new ArrayList<>();
String[] stringWithoutCommas=stringWithoutBraces.split(",");
for(String splited:stringWithoutCommas){
String eachElement=splited.substring(1,splited.length()-1);
loanidlist.add(Long.parseLong(eachElement));
}
Long[] loanid=(Long[]) loanidlist.toArray(new Long[loanidlist.size()]);