'Remove trailing substring from String in Java

I am looking to remove parts of a string if it ends in a certain string.

An example would be to take this string: "[email protected]"

And remove the @2x.png so it looks like: "am.sunrise.ios"

How would I go about checking to see if the end of a string contains "@2x.png" and remove it?



Solution 1:[1]

You could check the lastIndexOf, and if it exists in the string, use substring to remove it:

String str = "[email protected]";
String search = "@2x.png";

int index = str.lastIndexOf(search);
if (index > 0) {
    str = str.substring(0, index);
}

Solution 2:[2]

Assuming you have a string initialized as String file = "[email protected]";.

if(file.endsWith("@2x.png"))
    file = file.substr(0, file.indexOf("@2x.png"));

The endsWith(String) method returns a boolean determining if the string has a certain suffix. Depending on that you can replace the string with a substring of itself between the first character and before the index of the character that you are trying to remove.

Solution 3:[3]

private static String removeSuffixIfExists(String key, String suffix) {
    return key.endswith(suffix)
        ? key.substring(0, key.length() - suffix.length())
        : key; 
    }
}

String suffix = "@2x.png";
String key = "[email protected]";

String output = removeSuffixIfExists(key, suffix);

Solution 4:[4]

public static void main(String [] args){

    String word = "[email protected]";

    word = word.replace("@2x.png", "");

    System.out.println(word);
}

Solution 5:[5]

If you want to generally remove entire content of string from @ till end you can use

yourString = yourString.replaceAll("@.*","");

where @.* is regex (regular expression) representing substring starting with @ and having any character after it (represented by .) zero or more times (represented by *).

In case there will be no @xxx part your string will be unchanged.


If you want to change only this particular substring @2x.png (and not substirng like @3x.png) while making sure that it is placed at end of your string you can use

yourString = yourString.replaceAll("@2x\\.png$","");

where

  • $ represents end of string
  • \\. represents . literal (we need to escape it since like shown earlier . is metacharacter representing any character)

Solution 6:[6]

Since I was trying to do this on an ArrayList of items similarly styled I ended up using the following code:

    for (int image = 0; image < IBImages.size(); image++) {
        IBImages.set(image, IBImages.get(image).split("~")[0].split("@")[0].split(".png")[0]);
    }

If I have a list of images with the names

[am.sunrise.ios.png, [email protected], [email protected], am.sunrise.ios~ipad.png, [email protected]]

This allows me to split the string into 2 parts. For example, "am.sunrise.ios~ipad.png" will be split into "am.sunrise.ios" and "~ipad.png" if I split on "~". I can just get the first part back by referencing [0]. Therefore I get what I'm looking for in one line of code.

Note that image is "am.sunrise.ios~ipad.png"

Solution 7:[7]

You could use String.split():

public static void main(String [] args){
  String word = "[email protected]";
  String[] parts = word.split("@");
  if (parts.length == 2) {
    System.out.println("looks like user@host...");
    System.out.println("User: " + parts[0]);
    System.out.println("Host: " + parts[1]);
  }
}

Then you haven an array of Strings, where the first element contains the part before "@" and the second element the part after the "@".

Solution 8:[8]

Combining the answers 1 and 2:

String str = "[email protected]";
String search = "@2x.png";

if (str.endsWith(search)) {
  str = str.substring(0, str.lastIndexOf(search));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mureinik
Solution 2
Solution 3 Hannu Varjoranta
Solution 4 Christopher Bell
Solution 5
Solution 6
Solution 7
Solution 8