-2

I have string like element= "a||||b||||c" and I want to split like [a,b,c] by using

Pattern pattern=Pattern.compile("\\|\\|\\|\\|")
String[] splitedValues = pattern.split(element);

I got splitedValues =[a,b,c] size 3

If element=|||||||| i got splitedValues size 0

I want splitedValues =["","",""] with size 3

how can I achieve this?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Basi
  • 3,009
  • 23
  • 28

2 Answers2

0

you can try something like this.

     /**
 * @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;
}
Tejas
  • 433
  • 1
  • 4
  • 17
0

you can use String split function,

      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);

output

Data 
Data 
Data 
test |||||||| size :3
---------
Data a
Data b
Data c
test a||||b||||c size :3
Akash Shah
  • 596
  • 4
  • 17