0

I have an array of Strings that contains: Extra Water, Juice, and Extra Milk, so I am wondering how would I get rid of the extras and use the only second word in the string so that the expected output is Water, Juice, and Milk.

3
  • Your logic is not clear. Commented Mar 1, 2016 at 1:59
  • Is "Extra" the only unnecessary word or are there other such words? Commented Mar 1, 2016 at 1:59
  • I just want to use the second word if the first word is a duplicate in the array. So if the array is Extra Water, Juice, Extra Milk, Good Grape, Good Apple, Tasty Orange, the output would be Water, Juice, Milk, Grape, Apple, Tasty Orange Commented Mar 1, 2016 at 2:14

3 Answers 3

3

If all you want to do is remove a specific substring then:

String[] array = {"Extra Water", "Juice", "Extra Milk"};
array = Arrays.stream(array).map(s-> s.replaceAll("Extra", "")).toArray();

This uses Java 8 streams but you could do it just as simply with iteration.

Sign up to request clarification or add additional context in comments.

1 Comment

map(s -> s.replaceFirst("^Extra ", "")) would be probably more appropriate in this case though :)
2

Use String.split(' ') to split the string by a space, then check the result to see if the string length == 2. If so, then take the second element of the array, otherwise the first.

for( int i = 0; i < array.length; i++ ) {
    String[] parts = array[i].split(' ');
    if( parts.length == 2 ) {
        array[i] = parts[1];
    }
}

EDIT: If you want to remove all duplicate words, you could do the following using two passes over the array:

    // Pass 1 -- find all duplicate words
    Set<String> wordSet = new HashSet<>();
    Set<String> duplicateSet = new HashSet<>();
    for (int i = 0; i < array.length; i++) {
        String[] parts = array[i].split(" ");
        for (String part : parts) {
            if (!wordSet.contains(part)) {
                // Haven't seen this word before
                wordSet.add(part);
            } else {
                // This word is a duplicate word
                if (!duplicateSet.contains(part)) {
                    duplicateSet.add(part);
                }
            }
        }
    }

    // Pass 2 -- remove all words that are in the duplicate set
    for (int i = 0; i < array.length; i++) {
        String[] parts = array[i].split(" ");
        String dedupedString = "";
        for (String part : parts) {
            if (!duplicateSet.contains(part)) {
                dedupedString += part + " ";
            }
        }
        array[i] = dedupedString;
    }

Comments

0

Simply you need to iterate over each element of the array and replace the "Extra" in each element of the array and then trim the white spaces.

    String[] array = {"Extra Water", "Juice", "Extra Milk"};
    for (int i = 0; i < array.length; i++) {
        array[i] = array[i].replace("Extra", "").trim();
    }
    for (String each : array) {
        System.out.println(each);
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.