如何从字符串创建String []数组? [英] How to create a String[] array from a String?
本文介绍了如何从字符串创建String []数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在变量 coords 上有一个String,它具有String数组的值,但它不是数组(toString的结果).
I have a String on the variable coords which has the value of a String array, but it is not an array (outcome of toString).
String coords = "[[126,224],[255,232],[270,332],[106,444]]";
如何从此字符串创建字符串数组?数组没有#valueOf(据我所知),并且您不能轻易拆分字符.
How can you create a String array from this String? An array doesn't have a #valueOf (as far as I know) and you can't easily split the characters.
我正在寻找一个String数组,其中包含四个String作为自己的条目.例如."126,224","255,322" ..
I am looking for a String array which contains the four Strings as their own entries. E.g. "126,224", "255,322"..
推荐答案
最简单的方法之一是:
String[] array = coords.replaceAll("\\[\\[", "").replaceAll("\\]\\]", "").split("\\],\\[");
上面的代码将所有 [[[
和]]
替换为一个空字符,然后用],[
分割.
The above code replaces all [[
and ]]
with an empty character and then split by ],[
.
这篇关于如何从字符串创建String []数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文