62

A java String variable whose value is

String path = "http://cdn.gs.com/new/downloads/Q22010MVR_PressRelease.pdf.null"

I want to remove the last four characters i.e., .null. Which method I can use in order to split.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
Sangram Anand
  • 10,526
  • 23
  • 70
  • 103
  • 10
    Man, the `String` class itself has all required methods. Just type `path.` and spend 2 minutes looking at the code completion options. Really, habit to learn will pay in future 100 times. – Sergey Grinev Feb 03 '12 at 07:44
  • 6
  • possible duplicate of [How to remove the last character from a string?](http://stackoverflow.com/questions/7438612/how-to-remove-the-last-character-from-a-string) – Nateowami Oct 04 '14 at 12:31
  • @Nateowami `.null` if 5 characters, not 1. Also, the top answer in this question better addresses the problem that any of those answers. – Uyghur Lives Matter Oct 04 '14 at 16:14
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Billal Begueradj Jun 06 '19 at 07:32

6 Answers6

144

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
38
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"
Matthew Steven Monkan
  • 8,170
  • 4
  • 53
  • 71
7
path = path.substring(0, path.length() - 5);
juergen d
  • 201,996
  • 37
  • 293
  • 362
6

I am surprised to see that all the other answers (as of Sep 8, 2013) either involve counting the number of characters in the substring ".null" or throw a StringIndexOutOfBoundsException if the substring is not found. Or both :(

I suggest the following:

public class Main {

    public static void main(String[] args) {

        String path = "file.txt";
        String extension = ".doc";

        int position = path.lastIndexOf(extension);

        if (position!=-1)
            path = path.substring(0, position);
        else
            System.out.println("Extension: "+extension+" not found");

        System.out.println("Result: "+path);
    }

}

If the substring is not found, nothing happens, as there is nothing to cut off. You won't get the StringIndexOutOfBoundsException. Also, you don't have to count the characters yourself in the substring.

Ali
  • 56,466
  • 29
  • 168
  • 265
  • 1
    This is really the best answer, as it can be adapted to suit any suffix without using a hard limit. – n_b Apr 23 '15 at 22:10
4

If you like to remove last 5 characters, you can use:

path.substring(0,path.length() - 5)

( could contain off by one error ;) )

If you like to remove some variable string:

path.substring(0,path.lastIndexOf('yoursubstringtoremove));

(could also contain off by one error ;) )

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
1

Another way:

if (s.size > 5) s.reverse.substring(5).reverse

BTW, this is Scala code. May need brackets to work in Java.

Jus12
  • 17,824
  • 28
  • 99
  • 157
  • For the java version of this, you must create a `StringBuilder` based on the target string, and just as above, reverse it, delete the first 5 characters using `delete(0, 5)`, then reverse it again. Then turn it back into a string. – Subaru Tashiro Nov 11 '18 at 13:34