如何使用自定义模式拆分java中的字符串
我正在尝试使用如何使用自定义模式拆分java中的字符串,java,android,Java,Android,我正在尝试使用string.split(“[,\ \:]”)从该字符串获取位置数据 我怎样才能得到这样的数据 str[0] = 27.980194 str[1] = 46.090199 str[2] = 0.48 str[3] = 1 str[4] = 6 谢谢你的帮助 如果您只想保留数字(包括点分隔符),可以使用: String[] str = location.split("[^\\d\\.]+"); 您需要忽略数组中的第一个元素,它是一个空字符串 只有当数据名不包含数字或点时,这才有效
string.split(“[,\ \:]”)从该字符串获取位置数据代码>
我怎样才能得到这样的数据
str[0] = 27.980194
str[1] = 46.090199
str[2] = 0.48
str[3] = 1
str[4] = 6
谢谢你的帮助 如果您只想保留数字(包括点分隔符),可以使用:
String[] str = location.split("[^\\d\\.]+");
您需要忽略数组中的第一个元素,它是一个空字符串
只有当数据名不包含数字或点时,这才有效。如果您只想保留数字(包括点分隔符),可以使用:
String[] str = location.split("[^\\d\\.]+");
您需要忽略数组中的第一个元素,它是一个空字符串
只有当数据名不包含数字或点时,这才有效。快速脏:
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
List<String> strList = (List) Arrays.asList( location.split("[,\\:]"));
String[] str = new String[5];
int count=0;
for(String s : strList){
try {
Double d =Double.parseDouble(s);
str[count] = d.toString();
System.out.println("In String Array:"+str[count]);
count++;
} catch (NumberFormatException e) {
System.out.println("s:"+s);
}
}
String location=“$,纬度:27.980194,液化天然气:46.090199,速度:0.48,固定点:1,卫星:6,”;
List strList=(List)Arrays.asList(location.split(“[,\ \:]”);
字符串[]str=新字符串[5];
整数计数=0;
for(字符串s:strList){
试一试{
Double d=Double.parseDouble(s);
str[count]=d.toString();
System.out.println(“字符串数组中:“+str[count]);
计数++;
}捕获(数字格式){
System.out.println(“s:+s”);
}
}
又快又脏:
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
List<String> strList = (List) Arrays.asList( location.split("[,\\:]"));
String[] str = new String[5];
int count=0;
for(String s : strList){
try {
Double d =Double.parseDouble(s);
str[count] = d.toString();
System.out.println("In String Array:"+str[count]);
count++;
} catch (NumberFormatException e) {
System.out.println("s:"+s);
}
}
String location=“$,纬度:27.980194,液化天然气:46.090199,速度:0.48,固定点:1,卫星:6,”;
List strList=(List)Arrays.asList(location.split(“[,\ \:]”);
字符串[]str=新字符串[5];
整数计数=0;
for(字符串s:strList){
试一试{
Double d=Double.parseDouble(s);
str[count]=d.toString();
System.out.println(“字符串数组中:“+str[count]);
计数++;
}捕获(数字格式){
System.out.println(“s:+s”);
}
}
String location=“$,纬度:27.980194,液化天然气:46.090199,速度:0.48,固定点:1,卫星:6,”;
Matcher m=Pattern.compile(“\\d+\\.*\\d*”).Matcher(位置);
List allMatches=new ArrayList();
而(m.find()){
添加(m.group());
}
System.out.println(所有匹配项);
String location=“$,纬度:27.980194,液化天然气:46.090199,速度:0.48,固定点:1,卫星:6,”;
Matcher m=Pattern.compile(“\\d+\\.*\\d*”).Matcher(位置);
List allMatches=new ArrayList();
而(m.find()){
添加(m.group());
}
System.out.println(所有匹配项);
最好使用regexp IMHO。(\\d+。\\d+)|(\\d)
匹配您想要的所有数字。现在,您只需提取它们,最好使用regexp IMHO。(\\d+。\\d+)|(\\d)
匹配您想要的所有数字。现在您只需要提取它们谢谢,变量计数在哪里递增?谢谢,变量计数在哪里递增?。