Reputation: 497
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?
Upvotes: 9
Views: 20493
Reputation:
Assuming you have a string initialized as String file = "[email protected]";
.
if(file.endsWith("@2x.png"))
file = file.substring(0, file.lastIndexOf("@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 starting with the first character and ending before the index of the character that you are trying to remove.
Upvotes: 9
Reputation: 7880
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));
}
Upvotes: -1
Reputation: 189
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);
Upvotes: 10
Reputation: 497
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"
Upvotes: 1
Reputation: 3035
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 "@".
Upvotes: 0
Reputation: 124275
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)Upvotes: 1
Reputation: 312136
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);
}
Upvotes: 12
Reputation: 121
public static void main(String [] args){
String word = "[email protected]";
word = word.replace("@2x.png", "");
System.out.println(word);
}
Upvotes: 1