Remove the last part of a String in Java
Asked Answered
D

6

6

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 ...

Diaphony answered 23/3, 2011 at 16:50 Comment(2)
Would you mind to define what "the last part of the string" is?Cashman
Give a concrete example of a given input and a given output.Ditty
C
32

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(" "));
Clausius answered 23/3, 2011 at 16:53 Comment(2)
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.Zhao
It may not answer the OP, but this is the answer that I, and apparently 10 others, were looking for. Thanks :)Cerellia
T
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;

}
Testaceous answered 23/3, 2011 at 17:45 Comment(0)
C
1

Your whole code can be optimized to:

boolean foundmatch = y.startsWith(x);
y = foundmatch? x : "";
Cashman answered 23/3, 2011 at 16:57 Comment(0)
A
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#");
Acutance answered 19/5, 2015 at 13:40 Comment(0)
W
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;
Welby answered 23/3, 2011 at 16:56 Comment(3)
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.Zhao
:) maybe I live in another world, but how do you connect strings?Welby
StringBuilder sb = new StringBuilder(); for(...) { sb.append(s[i]); } return sb.toString();Zhao
N
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.

Nutting answered 23/3, 2011 at 16:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.