Java 删除字符串的最后两个字符
如何删除简单字符串的最后两个字符Java 删除字符串的最后两个字符,java,string,substring,Java,String,Substring,如何删除简单字符串的最后两个字符05 简单: "apple car 05" 代码 String[] lineSplitted = line.split(":"); String stopName = lineSplitted[0]; String stop = stopName.substring(0, stopName.length() - 1); String stopEnd = stopName.substring(0, stop.length() - 1); 拆分前的原始行“:”
05
简单:
"apple car 05"
代码
String[] lineSplitted = line.split(":");
String stopName = lineSplitted[0];
String stop = stopName.substring(0, stopName.length() - 1);
String stopEnd = stopName.substring(0, stop.length() - 1);
拆分前的原始行“:”
在删除最后一个空格的基础上减去
-2
或-3
public static void main(String[] args) {
String s = "apple car 05";
System.out.println(s.substring(0, s.length() - 2));
}
输出
apple car
使用
子字符串从指定的beginIndex开始,并延伸到索引处的字符(endIndex-1)您可以使用
子字符串
函数:
s.substring(0,s.length() - 2));
对于第一个0
,您对substring
说它必须从字符串的第一个字符开始,对于s.length()-2
,它必须在字符串结束前完成两个字符
有关子字符串
函数的更多信息,请参见:
这几乎是正确的,只需将最后一行更改为:
String stopEnd = stop.substring(0, stop.length() - 1); //replace stopName with stop.
或
您可以替换最后两行
String stopEnd = stopName.substring(0, stopName.length() - 2);
您可以使用以下方法删除最后一个
n
字符-
public String removeLast(String s, int n) {
if (null != s && !s.isEmpty()) {
s = s.substring(0, s.length()-n);
}
return s;
}
另一种解决方案是使用某种
regex
:
例如:
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"
您还可以尝试以下带有异常处理的代码。这里有一个方法
removeLast(String s,int n)
(它实际上是masud.m答案的修改版本)。您必须提供String
s以及要从最后一个char
中删除多少个removeLast(String s,int n)
函数。如果必须从最后一个字符中删除的char
s的数量大于给定的String
长度,则它会抛出一个带有自定义消息的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;
}
到目前为止,您尝试了什么?javadoc中的任何内容?我尝试了上面的代码,但我觉得这是错误的方法。@FastSnail:我正在拆分以在行乞时获取单词。如果我们增加字符串大小,则可能无法工作。所以我认为最好从最后一个答案中删除其他答案中的内容。@masud.m按照您的建议更新了答案。应该是
str.length-3
为什么Java不想清理这个功能?字符串解析广泛应用于各种规模的组织中。在Python中,它的操作非常简单,如s[:-2]
。“就是这样。”桑卡普,Python更容易学习,但它速度慢,有时不可预测。在Java中,您可以键入更多的代码,但可以得到快速可靠的结果。
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"
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;
}