Java 删除字符串的最后两个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30708036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:02:45  来源:igfitidea点击:

Delete the last two characters of the String

javastringsubstring

提问by TheBook

How can I delete the last two characters 05of the simple string?

如何删除05简单字符串的最后两个字符?

Simple:

简单的:

"apple car 05"

Code

代码

String[] lineSplitted = line.split(":");
String stopName = lineSplitted[0];
String stop =   stopName.substring(0, stopName.length() - 1);
String stopEnd = stopName.substring(0, stop.length() - 1);

orginal line before splitting ":"

拆分“:”之前的原始行

apple car 04:48 05:18 05:46 06:16 06:46 07:16 07:46 16:46 17:16 17:46 18:16 18:46 19:16

采纳答案by Ankur Singhal

Subtract -2or -3basis of removing last space also.

减去-2-3删除最后一个空格的基础也。

 public static void main(String[] args) {
        String s = "apple car 05";
        System.out.println(s.substring(0, s.length() - 2));
    }

Output

输出

apple car

回答by Isuru Gunawardana

Use String.substring(beginIndex, endIndex)

使用String.substring(beginIndex, endIndex)

str.substring(0, str.length() - 2);

The substring begins at the specified beginIndex and extends to the character at index (endIndex - 1)

子字符串从指定的 beginIndex 开始并扩展到索引处的字符 (endIndex - 1)

回答by Francisco Romero

You can use substringfunction:

您可以使用substring功能:

s.substring(0,s.length() - 2));

With the first 0, you say to substringthat it has to start in the first character of your string and with the s.length() - 2that it has to finish 2 characters before the String ends.

对于 first 0,您说它必须substring从字符串的第一个字符开始s.length() - 2,并且必须在字符串结束之前完成 2 个字符。

For more information about substringfunction you can see here:

有关substring功能的更多信息,您可以在此处查看:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

回答by Saif

It was almost correct just change your last line like:

几乎是正确的,只需更改您的最后一行,例如:

String stopEnd = stop.substring(0, stop.length() - 1); //replace stopName with stop.

OR

或者

you can replace your last two lines;

您可以替换最后两行;

String stopEnd =   stopName.substring(0, stopName.length() - 2);

回答by masud.m

You may use the following method to remove last ncharacter -

您可以使用以下方法删除最后一个n字符 -

public String removeLast(String s, int n) {
    if (null != s && !s.isEmpty()) {
        s = s.substring(0, s.length()-n);
    }
    return s;
}

回答by nafas

An alternative solution would be to use some sort of regex:

另一种解决方案是使用某种regex

for example:

例如:

    String s = "apple car 04:48 05:18 05:46 06:16 06:46 07:16 07:46 16:46 17:16 17:46 18:16 18:46 19:16";
    String results=  s.replaceAll("[0-9]", "").replaceAll(" :", ""); //first removing all the numbers then remove space followed by :
    System.out.println(results); // output 9
    System.out.println(results.length());// output "apple car"

回答by Razib

You may also try the following code with exception handling. Here you have a method removeLast(String s, int n)(it is actually an modified version of masud.m's answer). You have to provide the Strings and how many charyou want to remove from the last to this removeLast(String s, int n)function. If the number of chars have to remove from the last is greater than the given Stringlength then it throws a StringIndexOutOfBoundExceptionwith a custom message -

您也可以尝试使用以下代码进行异常处理。在这里你有一个方法removeLast(String s, int n)(它实际上是masud.m答案的修改版本)。您必须提供Strings 以及char要从最后一个删除到此removeLast(String s, int n)函数的数量。如果char必须从最后一个删除的s数量大于给定的String长度,则它会抛出StringIndexOutOfBoundException带有自定义消息的s -

public String removeLast(String s, int n) throws StringIndexOutOfBoundsException{

        int strLength = s.length();

        if(n>strLength){
            throw new StringIndexOutOfBoundsException("Number of character to remove from end is greater than the length of the string");
        }

        else if(null!=s && !s.isEmpty()){

            s = s.substring(0, s.length()-n);
        }

        return s;

    }