简体   繁体   English

删除Java String变量的最后一个字符

[英]Remove the last chars of the Java String variable

A java String variable whose value is 一个java String变量,其值为

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

I want to remove the last four characters ie, .null . 我想删除最后四个字符,即.null Which method I can use in order to split. 我可以使用哪种方法进行拆分。

I think you want to remove the last five characters ('.', 'n', 'u', 'l', 'l'): 我想你要删除最后五个字符('。','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. 请注意您需要如何使用返回值 - 字符串是不可变的,因此substring (和其他方法)不会更改现有字符串 - 它们返回对具有适当数据的字符串的引用。

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: 我的猜测是你只得到了“.null”,因为其他一些代码正在做这样的事情:

path = name + "." + extension;

where extension is null. 其中extension是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.) (如问题评论中所述,你真的应该查看String API 。它是Java中最常用的类之一,所以没有理由不熟悉它。)

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"
path = path.substring(0, path.length() - 5);

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. 我很惊讶地看到所有其他答案(截至2013年9月8日)或者涉及计算子字符串".null"的字符数,或者如果找不到子字符串则抛出StringIndexOutOfBoundsException 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 . 你不会得到StringIndexOutOfBoundsException Also, you don't have to count the characters yourself in the substring. 此外,您不必在子字符串中自己计算字符数。

If you like to remove last 5 characters, you can use: 如果您想删除最后5个字符,可以使用:

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 ;) ) (也可能包含一个错误;))

Another way: 其他方式:

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

BTW, this is Scala code. 顺便说一下,这是Scala代码。 May need brackets to work in Java. 可能需要括号才能在Java中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM