replace will replace all instances of a letter. All you need to do is use substring():
public String method(String str) {
if (str != null && str.length() > 0 && str.charAt(str.length() - 1) == 'x') {
str = str.substring(0, str.length() - 1);
}
return str;
}
Answer from Gyan on Stack Overflowreplace will replace all instances of a letter. All you need to do is use substring():
public String method(String str) {
if (str != null && str.length() > 0 && str.charAt(str.length() - 1) == 'x') {
str = str.substring(0, str.length() - 1);
}
return str;
}
Why not just one liner?
public static String removeLastChar(String str) {
return removeChars(str, 1);
}
public static String removeChars(String str, int numberOfCharactersToRemove) {
if(str != null && !str.trim().isEmpty()) {
return str.substring(0, str.length() - numberOfCharactersToRemove);
}
return "";
}
Full Code
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String s1 = "Remove Last CharacterY";
String s2 = "Remove Last Character2";
String s3 = "N";
String s4 = null;
String s5 = "";
System.out.println("After removing s1==" + removeLastChar(s1) + "==");
System.out.println("After removing s2==" + removeLastChar(s2) + "==");
System.out.println("After removing s3==" + removeLastChar(s3) + "==");
System.out.println("After removing s4==" + removeLastChar(s4) + "==");
System.out.println("After removing s5==" + removeLastChar(s5) + "==");
}
public static String removeLastChar(String str) {
return removeChars(str, 1);
}
public static String removeChars(String str, int numberOfCharactersToRemove) {
if(str != null && !str.trim().isEmpty()) {
return str.substring(0, str.length() - numberOfCharactersToRemove);
}
return "";
}
}
Demo
How to eliminate last character in a java string - Stack Overflow
Remove the last chars of the Java String variable - Stack Overflow
Removing last 3 characters from a string? (SQLite)
removing all non-letter characters from a string? ((using regex))
Videos
All you need is
System.out.println(s.substring(0,s.length()-1));
There is no need to actually fetch the last character.
Signature of String.substring
substring(beginIndex,lastIndex)
Javadoc
So, if you put character in parameter, it converted 'l' into ASCII code 108 automatically that's why you got StringIndexOutOfBoundsException at end 108.
So, just use like that -
System.out.println(s.substring(0,s.length()-1));
I think you want to remove the last five characters ('.', 'n', 'u', 'l', 'l'):
path = path.substring(0, path.length() - 5);
Note how you need to use the return value - strings are immutable, so substring (and other methods) don't change the existing string - they return a reference to a new string with the appropriate data.
Or to be a bit safer:
if (path.endsWith(".null")) {
path = path.substring(0, path.length() - 5);
}
However, I would try to tackle the problem higher up. My guess is that you've only got the ".null" because some other code is doing something like this:
path = name + "." + extension;
where extension is null. I would conditionalise that instead, so you never get the bad data in the first place.
(As noted in a question comment, you really should look through the String API. It's one of the most commonly-used classes in Java, so there's no excuse for not being familiar with it.)
import org.apache.commons.lang3.StringUtils;
// path = "http://cdn.gs.com/new/downloads/Q22010MVR_PressRelease.pdf.null"
StringUtils.removeEnd(path, ".null");
// path = "http://cdn.gs.com/new/downloads/Q22010MVR_PressRelease.pdf"