2

I want to split lines of words on the letter A. For example: AoAoAoA should result in [o, o, o], which it does with my code. But the problem is where the word has no characters between two A's, such as: AoAA that will only result in [o]. How can I adjust my code in order for the result to be [o,] instead?

This is because I want to see if there are equal amount of the same characters between the delimiter. And if there are, something will happen, and if not something else will happen. "AoAA" does not have equal amounts of "o" between the A's and should therefore be treated as such.

List<String> myList = new ArrayList <>();
String row = scanner.nextLine();
String [] splitString = row.split ("A");

for (String s : splitString) {
if (!s.equals(splitString[0])) {
myList.add(s);
}}

ps. I understand that my questions seems to be a duplicate, I have tried several of the solutions that are similar to my problem, but for some reason they do not solve my issue. For example, by simply adding split (string, int), has not solved anything for me.

a.e.m.n
  • 33
  • 6
  • `"AoAA".split("A");` will result in `[, o]` not `[o]` or `[o, ]`. – Samuel Philipp Jun 23 '19 at 13:36
  • @SamuelPhilipp Yes I fixed that by simply excluding the first part. Since my words will begin and end with A's I thought it would be wise to exclude the first part since that one will always be empty. – a.e.m.n Jun 23 '19 at 13:40
  • @Bubletan I looked at that one and tried their solution, but nothing changed, it still doesn't save the empty part in the list. – a.e.m.n Jun 23 '19 at 13:45
  • 1
    `"AoAA".split("A")` results `["", "o"]` and `"AoAA".split("A", -1)` results `["", "o", "", ""]`. If you drop the first and the last string, you get `["o", ""]`, which is what you wanted. – Bubletan Jun 23 '19 at 13:56
  • So you want to keep the empty strings from the split, but not the empty strings at the end or at the start? – Sweeper Jun 23 '19 at 13:59
  • @Sweeper Yes exactly, I want to keep all empty strings except for the first and last since those are "irrelevant". – a.e.m.n Jun 23 '19 at 14:06
  • @Bubletan I tried "dropping" the first string by simply using (!s.equals(splitString[0])) as I wrote in my question, is that the wrong way to do it? Because it seems as if that code effects the empty strings that are not in the 0st index as well. – a.e.m.n Jun 23 '19 at 14:45

2 Answers2

0

One way that I can think of is to first remove any leading or trailing As before you actually split the string:

String[] result = input.replaceAll("^A|A$", "")
                       .split("A", -1);

This is because it is the leading and trailing A's that are causing the empty splits to occur at the two ends of the string. If we remove those A's first, then we won't get any of the undesirable splits.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Use the split(String, int) version of split():

List<String> myList = Array.asList(input.replaceAll("^A*", "").split("A", -1));

As per the javadoc:

If the limit is negative then the pattern will be applied as many times as possible and the array can have any length.

Bohemian
  • 412,405
  • 93
  • 575
  • 722