-4

How to create an array from a string with two respective data items.

String str="USA*2*Japan*8^2*India*5^4^2*Germany*5*";

Here, I want to create an array of two items in .

Like this:

Arraylist<string> arr= [USA*2,Japan*8^2, India*5^4^2,Germany*5];

Here * is indicating main items and ^ is indicating sub items.

No_Name_Code
  • 299
  • 10
  • 20

1 Answers1

1

You are using the * to separate "main items" but it can also be inside the main item.

Your requirements look odd, but lets assume for the sake of argument that you are getting your input data like this and you want to split it like you suggested. That means that every * that is preceded by a number is a separator, but a * that is not preceded by a number is not.

You can achieve that using regular expressions: (with a positive look-behind expression (?<=expr)

    String str = "USA*2*Japan*8^2*India*5^4^2*Germany*5";
    List<String> lst = Arrays.asList(Pattern.compile("(?<=\\d)\\*").split(str));
    System.out.println(lst);

Prints:

[USA*2, Japan*8^2, India*5^4^2, Germany*5]

After further clarification in the comment below, it seems that the problem is more generic than the initial example; the question becomes:

How do I split a string on a separator, but only after 2 occurrences of the separator.

Although it's possible to do with a regex, it may be easier to do and understand in a for loop like this:

public static List<String> split(String str, char splitChar, int afterOccurrences) {
    List<String> lst = new ArrayList<>();
    int occurrencesSeen = 0;
    int start = 0;
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch == splitChar) {
            occurrencesSeen++;
            if (occurrencesSeen >= afterOccurrences) {
                lst.add(str.substring(start, i));
                start = i + 1;
                occurrencesSeen = 0;
            }
        }
    }
    if (start < str.length() - 1)
        lst.add(str.substring(start));
    return lst;
}

public static void main(String[] args) {
    String str = "USA*2*Japan*8^2*India*5^4^2*Germany*5";
    System.out.println(split(str, '*', 2));
}

This method also allows you to split after 3 or any other number of occurrences.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Oddly enough the comment below from the OP is now deleted, but it indicated what I mention above. – Erwin Bolwidt Jan 24 '14 at 08:04
  • Sorry for comment delete, you answer works for me, then comment is not necessarily, Now Here 8 items, then splitting two at one, if we want 4 at one may be any number then what to do:[USA*2*Japan*8^2, India*5^4^2* Germany*5]. How to find out how many item we take for splitting? – No_Name_Code Jan 24 '14 at 08:06
  • Thank you it works perfectly. but i have last position also * Like this -> USA*2*Japan*8^2*India*5^4^2*Germany*5 . but creating extra one item, why. – No_Name_Code Jan 24 '14 at 08:13
  • Are you asking how to count the number of `*` in the string? Have a look at this question, it should explain: http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string (although this also works, somewhat less efficiently: `System.out.println(str.split("\\*").length);` – Erwin Bolwidt Jan 24 '14 at 08:14
  • Finally, thank so much it is really great helpful for me. – No_Name_Code Jan 24 '14 at 08:20
  • About the extra item - I added an extra guard statement in my answer above `if (start < str.length() - 1)` to take that into account – Erwin Bolwidt Jan 24 '14 at 08:29