【问题标题】:Delete the last two characters of the String [duplicate]删除字符串的最后两个字符[重复]
【发布时间】:2015-08-22 20:29:51
【问题描述】:

如何删除简单字符串的最后两个字符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);

分割前的原行“:”

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

【问题讨论】:

  • 到目前为止你尝试了什么?
  • javadoc 中有什么内容吗?
  • 我已经尝试了上面的代码,但我觉得它是错误的方式。
  • @FastSnail:我正在拆分以获取行首的单词。

标签: java string substring


【解决方案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

【讨论】:

  • 为什么 Java 不想清理这个功能?字符串解析在各种规模的组织中被广泛使用。在 Python 中,它就像 s[:-2] 一样简单。就是这样。
  • @Sankalp,Python 更容易学习,但速度很慢,有时无法预测。在 Java 中,您需要输入更多代码,但您会得到快速可靠的结果。
【解决方案2】:

使用String.substring(beginIndex, endIndex)

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

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

【讨论】:

  • 如果我们增加字符串大小,它可能不起作用。所以我认为最好从其他答案中看到的最后删除。
  • @masud.m 按照您的建议更新了答案。
  • 应该是str.length - 3
【解决方案3】:

你可以使用substring函数:

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

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

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

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

【讨论】:

    【解决方案4】:

    这几乎是正确的,只需将最后一行更改为:

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

    您可以替换最后两行;

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

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        另一种解决方案是使用某种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"
        

        【讨论】:

          【解决方案7】:

          您也可以尝试使用以下带有异常处理的代码。在这里,您有一个方法removeLast(String s, int n)(它实际上是 ma​​sud.m 答案的修改版本)。您必须提供 String s 以及要从最后一个到此 removeLast(String s, int n) 函数删除多少 char。如果必须从最后一个删除的chars 的数量大于给定的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;
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-02-22
            • 1970-01-01
            • 2016-01-21
            • 2011-08-01
            • 2019-10-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多