4

My Code:

String Y = "part1 part2 part3", X = "part1";

boolean foundMatch = false;
while(!foundMatch) {
    foundMatch = Y.equals(X);
    if(foundMatch) {
        break;
    }
    else {
        Y = useSplitToRemoveLastPart(Y);
        if(Y.equals("")) {
            break;
        }
    }

Implementation of useSplitToRemoveLastPart()

private static String useSplitToRemoveLastPart(String y) {
    // What goes here?
    // It should chop the last part of the String...
}

Can anyone help ...

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Praneel PIDIKITI
  • 18,677
  • 13
  • 41
  • 60

6 Answers6

31

If you want part3 to be removed and provided that all the words are separated by space

String str ="part1 part2 part3";

String result = str.substring(0,str.lastIndexOf(" "));
Venkat Ramana
  • 508
  • 1
  • 6
  • 16
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    The instructions implicitly require the use of `split()` which this solution does not. Not going to downvote, because I find this method to be superior (for the real world) but it doesn't meet the requirements set forth by the name of the method. – corsiKa Mar 23 '11 at 17:01
  • 2
    It may not answer the OP, but this is the answer that I, and apparently 10 others, were looking for. Thanks :) – modulitos Oct 11 '14 at 00:48
3

If you really want to use split:

private static String useSplitToRemoveLastPart(String str) {
    String[] arr = str.split(" ");
    String result = "";
    if (arr.length > 0) {
        result = str.substring(0, str.lastIndexOf(" " + arr[arr.length-1]));
    }
    return result;

}
lukastymo
  • 26,145
  • 14
  • 53
  • 66
1
public String removeLastSubstring(String target, String toRemove){
    int idx = target.lastIndexOf(toRemove);
    target = target.substring(0, idx) + target.substring(idx + toRemove.length());
    return target;
}

You only need to pass it your target and the LAST substring you want to remove, example:

String s = "123 #abc# 456";
s = removeLastSubstring(s, "#abc#");
Tom
  • 16,842
  • 17
  • 45
  • 54
Alpha2k
  • 2,212
  • 7
  • 38
  • 65
1

Your whole code can be optimized to:

boolean foundmatch = y.startsWith(x);
y = foundmatch? x : "";
Ingo
  • 36,037
  • 5
  • 53
  • 100
0

If you want to do it using split, then you can do:

String s[] = Y.split(" ");
String n = "";
for (int i = 0; i < s.length - 1; i++)
        n+= s[i];
return n;
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 2
    I want to +1 for solving OPs problem with the given instruction, but I also want to -1 for encouraging the use of `string += otherString` in a loop. – corsiKa Mar 23 '11 at 17:02
  • 1
    :) maybe I live in another world, but how do you connect strings? – MByD Mar 23 '11 at 20:01
  • `StringBuilder sb = new StringBuilder(); for(...) { sb.append(s[i]); } return sb.toString();` – corsiKa Mar 23 '11 at 20:04
0

By the way, If the method you need to build is called useSplitToRemoveLastPart(String t), then I'd suggest you to use split to remove last part.

Take a look here.

abahgat
  • 13,360
  • 9
  • 35
  • 42