我有像 element= "a||||b||||c"
这样的字符串,我想通过使用
Pattern pattern=Pattern.compile("\|\|\|\|")
String[] splitedValues = pattern.split(element);
我得到了splitedValues =[a,b,c]
大小3
如果element=||||||||
我得到了splitedValues
大小0
我想要 splitedValues =["","",""]
with 大小3
我该如何实现?
您可以使用字符串拆分功能,
String test = "||||||||";
String[] testArray = test.split("\|\|\|\|", -1);
for (String t : testArray) {
System.out.println("Data " + t);
}
System.out.println("test " + test + " size :" + testArray.length);
test = "a||||b||||c";
System.out.println("---------");
testArray = test.split("\|\|\|\|", -1);
for (String t : testArray) {
System.out.println("Data " + t);
}
System.out.println("test " + test + " size :" + testArray.length);
输出
Data
Data
Data
test |||||||| size :3
---------
Data a
Data b
Data c
test a||||b||||c size :3
您可以尝试这样的东西。
/**
* @param rgbColor/argbColor This function take "rgb(123,23,23)/argb(123,12,12,12)" as input String and returns
* actual Color object for its equivalent rgb value
*/
public static int parseRgbColor(@NonNull String rgbColor) {
Pattern rgbColorPattern = Pattern.compile("rgb *\( *([0-9]+), *([0-9]+), *([0-9]+) *\)");
Pattern argbColorPattern = Pattern.compile("argb *\( *([0-9]+), *([0-9]+), *([0-9]+), *([0-9]+) *\)");
Matcher colorMatch = rgbColorPattern.matcher(rgbColor.toLowerCase());
if (colorMatch.matches()) {
return Color.rgb(Integer.valueOf(colorMatch.group(1)), Integer.valueOf(colorMatch.group(2)), Integer.valueOf(colorMatch.group(3)));
} else {
colorMatch = argbColorPattern.matcher(rgbColor.toLowerCase());
if (colorMatch.matches()) {
return Color.argb(Integer.valueOf(colorMatch.group(1)), Integer.valueOf(colorMatch.group(2)), Integer.valueOf(colorMatch.group(3)), Integer.valueOf(colorMatch.group(4)));
}
}
return -1;
}