【问题标题】:Delete everything after part of a string删除字符串的一部分之后的所有内容
【发布时间】:2012-08-29 22:30:59
【问题描述】:

我有一个由三部分组成的字符串。我希望字符串是(更改)这个词,一个单独的部分(不更改)和最后一个更改的部分。我想删除分隔部分和结尾部分。分隔部分是“ - ”所以我想知道是否有办法删除字符串的某个部分之后的所有内容。

这种情况的一个例子是,如果我想把这个:“堆栈溢出 - 问东西的地方”变成这个:“堆栈溢出”。任何帮助表示赞赏!

【问题讨论】:

    标签: java android string replace


    【解决方案1】:

    例如,你可以这样做:

    String result = input.split("-")[0];
    

    String result = input.substring(0, input.indexOf("-"));
    

    (并添加相关的错误处理)

    【讨论】:

    • 如果主字符串是这样的:/name/nm1243456/lkbfwugbòwougs 我只想保留 nm123456,你知道吗? (我不能像 input.substring(6, directorImdbId.length() - 15) 这样使用 s.th,因为字符串最后一部分的字符数正在变化(可能是 16 或 17 个字符)
    【解决方案2】:

    也许这就是你要找的东西:

    String str="Stack Overflow - A place to ask stuff";
    
    String newStr = str.substring(0, str.indexOf("-"));
    

    【讨论】:

    • 保留匹配字符串之后的所有内容。
    【解决方案3】:

    这将满足您的需要:

    newValue = oldValue.substring(0, oldValue.indexOf("-");
    

    【讨论】:

      【解决方案4】:

      你可以用这个

      String mysourcestring = "developer is - development";
      String substring = mysourcestring.substring(0,mysourcestring.indexOf("-"));
      

      应该写成“开发者是-”

      【讨论】:

      • 正在寻找一种解决方案来忽略子字符串之后的字符串中的所有内容,而您的回答启发了我想出String substr = mysourcestring.substring(0,mysourcestring.indexOf("searchSeq")+"searchSeq".length()); 例如,如果我需要在确定的子字符串之后忽略 URL 中的所有内容,这可能会有所帮助。
      【解决方案5】:

      我为所有方法创建了示例程序,SubString 似乎是最快的。

      Using builder : 54
      Using Split : 252
      Using Substring  : 10
      

      下面是示例程序代码

                  for (int count = 0; count < 1000; count++) {
              // For JIT
          }
          long start = System.nanoTime();
          //Builder
          StringBuilder builder = new StringBuilder(
                  "Stack Overflow - A place to ask stuff");
          builder.delete(builder.indexOf("-"), builder.length());
          System.out.println("Using builder : " + (System.nanoTime() - start)
                  / 1000);
          start = System.nanoTime();
          //Split
          String string = "Stack Overflow - A place to ask stuff";
          string.split("-");
          System.out.println("Using Split : " + (System.nanoTime() - start)
                  / 1000);
          //SubString
          start = System.nanoTime();
          String string1 = "Stack Overflow - A place to ask stuff";
          string1.substring(0, string1.indexOf("-"));
          System.out.println("Using Substring : " + (System.nanoTime() - start)
                  / 1000);
          return null;
      

      【讨论】:

        【解决方案6】:

        apache commons StringUtils 提供了一个substringBefore 方法

        StringUtils.substringBefore("Stack Overflow - A place to ask stuff", " - ")

        【讨论】:

        • 这很好用。我希望大多数重要的项目都引用 Apache Commons,这样可以避免容易出错的子字符串/索引
        【解决方案7】:
        String line = "deltaasm:/u01/app/oracle/product/11.2.0/dbhome_1:N        # line addred by agent";
        
            String rep = "deltaasm:";
            String after = "";
        
            String pre = ":N";
            String aft = "";
            String result = line.replaceAll(rep, after);
            String finalresult = result.replaceAll(pre, aft);
            System.out.println("Result***************" + finalresult);
        
            String str = "deltaasm:/u01/app/oracle/product/11.2.0/dbhome_1:N        # line addred by agent";
        
            String newStr = str.substring(0, str.indexOf("#"));
            System.out.println("======" + newStr);
        

        【讨论】:

          【解决方案8】:

          你可以在我的 utils 方法中执行此操作..

          public static String makeTwoPart(String data, String cutAfterThisWord){
              String result = "";
          
              String val1 = data.substring(0, data.indexOf(cutAfterThisWord));
          
              String va12 = data.substring(val1.length(), data.length());
          
              String secondWord = va12.replace(cutAfterThisWord, "");
          
              Log.d("VAL_2", secondWord);
          
              String firstWord = data.replace(secondWord, "");
          
              Log.d("VAL_1", firstWord);
          
              result = firstWord + "\n" + secondWord;
          
          
              return result;
          }`
          

          【讨论】:

            【解决方案9】:

            安全删除直到一个字符串的干净方法,如果令牌可能存在或可能不存在,则保留搜索到的部分。

            String input = "Stack Overflow - A place to ask stuff";
            String token = " - ";
            String result = input.contains(token)
              ? token + StringUtils.substringBefore(string, token)
              : input;
            // Returns "Stack Overflow - "
            

            Apache StringUtils 函数为 null、空且不匹配

            【讨论】:

              【解决方案10】:

              Kotlin 解决方案

              使用 Kotlin 内置的substringBefore 函数(Documentation):

              var string = "So much text - no - more"
              string = string.substringBefore(" - ") // "So much text"
              

              它还有一个可选的第二个参数,如果没有找到分隔符,它是返回值。默认值为原始字符串

              string.substringBefore(" - ", "fail")  // "So much text"
              string.substringBefore(" -- ", "fail") // "fail"
              string.substringBefore(" -- ")         // "So much text - no - more"
              

              【讨论】:

                猜你喜欢
                • 2012-10-19
                • 1970-01-01
                • 2022-08-19
                • 2019-02-11
                • 1970-01-01
                • 1970-01-01
                • 2017-04-03
                • 1970-01-01
                相关资源
                最近更新 更多