This question already has an answer here:
这个问题在这里已有答案:
- How to remove the last character from a string? 24 answers
如何从字符串中删除最后一个字符? 24个答案
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
7 个解决方案
#1
50
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
#2
14
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开始并扩展到index处的字符(endIndex - 1)
#3
3
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;
}
#4
1
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.
对于第一个0,你要说子串必须从你的字符串的第一个字符开始,并且使用s.length() - 2,它必须在字符串结束之前完成2个字符。
For more information about substring
function you can see here:
有关子字符串函数的更多信息,请参见此处:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
#5
1
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以及要从最后一个removeLast(String s,int n)函数中删除的char数。如果必须从最后删除的字符数大于给定的字符串长度,则它会抛出带有自定义消息的StringIndexOutOfBoundException -
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;
}
#6
0
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);
#7
0
An alternative solution would be to use some sort of 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"
#1
50
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
#2
14
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开始并扩展到index处的字符(endIndex - 1)
#3
3
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;
}
#4
1
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.
对于第一个0,你要说子串必须从你的字符串的第一个字符开始,并且使用s.length() - 2,它必须在字符串结束之前完成2个字符。
For more information about substring
function you can see here:
有关子字符串函数的更多信息,请参见此处:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
#5
1
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以及要从最后一个removeLast(String s,int n)函数中删除的char数。如果必须从最后删除的字符数大于给定的字符串长度,则它会抛出带有自定义消息的StringIndexOutOfBoundException -
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;
}
#6
0
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);
#7
0
An alternative solution would be to use some sort of 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"