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
Delete the last two characters of the String
提问by TheBook
How can I delete the last two characters 05
of 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 -2
or -3
basis 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 substring
function:
您可以使用substring
功能:
s.substring(0,s.length() - 2));
With the first 0
, you say to substring
that it has to start in the first character of your string and with the s.length() - 2
that it has to finish 2 characters before the String ends.
对于 first 0
,您说它必须substring
从字符串的第一个字符开始s.length() - 2
,并且必须在字符串结束之前完成 2 个字符。
For more information about substring
function 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 n
character -
您可以使用以下方法删除最后一个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 String
s and how many char
you want to remove from the last to this removeLast(String s, int n)
function. If the number of char
s have to remove from the last is greater than the given String
length then it throws a StringIndexOutOfBoundException
with a custom message -
您也可以尝试使用以下代码进行异常处理。在这里你有一个方法removeLast(String s, int n)
(它实际上是masud.m答案的修改版本)。您必须提供String
s 以及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;
}