【问题标题】:Extract substring from a string从字符串中提取子字符串
【发布时间】:2011-07-21 20:12:39
【问题描述】:

在android中从字符串中提取子字符串的最佳方法是什么?

【问题讨论】:

    标签: android string substring


    【解决方案1】:

    如果你知道开始和结束索引,你可以使用

    String substr=mysourcestring.substring(startIndex,endIndex);
    

    如果您想从特定索引中获取子字符串直到结束,您可以使用:

    String substr=mysourcestring.substring(startIndex);
    

    如果您想从特定字符获取子字符串直到结束,您可以使用:

    String substr=mysourcestring.substring(mysourcestring.indexOf("characterValue"));
    

    如果您想在 特定字符之后获取子字符串,请将那个数字添加到 .indexOf(char):

    String substr=mysourcestring.substring(mysourcestring.indexOf("characterValue") + 1);
    

    【讨论】:

    • 假设我想要字符串中某个字符的索引,我可以使用 .indexof("char")。但在我的情况下,有时该字符可能不在字符串中。然后我想得到一个空字符串。如何处理这个错误?
    • 只测试返回值。类似于: if (str.indexOf("char") > 0)
    • 很好的答案,但子字符串必须全部小写。所以它是 substring() 而不是 subString()
    • Android 文档是一个更好的来源: String substring (int beginIndex, int endIndex) 返回一个字符串,它是这个字符串的子字符串。子字符串从指定的 beginIndex 开始并延伸到索引 endIndex - 1 处的字符。因此子字符串的长度是 endIndex-beginIndex。
    【解决方案2】:

    substring()

    str.substring(startIndex, endIndex); 
    

    【讨论】:

    • 简单完美
    【解决方案3】:

    这是一个真实的例子:

    String hallostring = "hallo";
    String asubstring = hallostring.substring(0, 1); 
    

    在示例中,asubstring 将返回:h

    【讨论】:

    • 这个答案隐含地添加了其他两个更流行的答案中缺少的信息,即字符串是 0,而不是基于 1,并且值得更多的爱。
    • 重要的不是基础。 substring 采用从 startIndex 位置开始并延伸到但不包括 endIndex 的部分字符串。
    • 基地不重要???知道索引从零到 string.length() 以及返回的最后一个字符是 endIndex-1 索引的那个是令人难以置信的,你不是说吗?
    【解决方案4】:

    还有另一种方法,如果你想在一个字符之前和之后获取子字符串

    String s ="123dance456";
    String[] split = s.split("dance");
    String firstSubString = split[0];
    String secondSubString = split[1];
    

    检查这篇文章- how to find before and after sub-string in a string

    【讨论】:

      【解决方案5】:

      substring(int startIndex, int endIndex)

      如果不指定endIndex,该方法将返回所有 startIndex 中的字符。

      startIndex : 包含起始索引

      endIndex : 结束索引是唯一的

      例子:

      String str = "abcdefgh"

      str.substring(0, 4) => abcd

      str.substring(4, 6) => ef

      str.substring(6) => gh

      【讨论】:

      • 定义 (0, 4) 领先 abcd 这是最好的答案! (0,0) 或 (1,1) 会仅显示结果“a”吗?
      • @Bay 当 startIndex = endIndex 时,返回一个空字符串。
      【解决方案6】:

      您可以使用此代码

          public static String getSubString(String mainString, String lastString, String startString) {
          String endString = "";
          int endIndex = mainString.indexOf(lastString);
          int startIndex = mainString.indexOf(startString);
          Log.d("message", "" + mainString.substring(startIndex, endIndex));
          endString = mainString.substring(startIndex, endIndex);
          return endString;
      }
      

      在这个mainString 是一个超级字符串.like “I_AmANDROID.开发者” 而lastString 是一个类似于“。”的字符串。 startString 就像“_”。 所以这个function 返回“AmANDROID”。 享受你的代码时间。:)

      【讨论】:

        【解决方案7】:

        使用来自 android 的文本类:
        TextUtils.substring (charsequence source, int start, int end)

        【讨论】:

          【解决方案8】:

          您可以使用 subSequence ,它与 C 中的 substr 相同

           Str.subSequence(int Start , int End)
          

          【讨论】:

          • C++ 中的 substr 是用 START 和 LENGTH 定义的,而不是 startIndex 和 endIndex: string substr (size_t pos = 0, size_t len = npos) const;
          【解决方案9】:

          当找到匹配模式的子字符串多次出现时

              String input_string = "foo/adsfasdf/adf/bar/erqwer/";
              String regex = "(foo/|bar/)"; // Matches 'foo/' and 'bar/'
          
              Pattern pattern = Pattern.compile(regex);
              Matcher matcher = pattern.matcher(input_string);
          
              while(matcher.find()) {
                String str_matched = input_string.substring(matcher.start(), matcher.end());
                  // Do something with a match found
              }
          

          【讨论】:

            【解决方案10】:

            在 Android 中获取子字符串的最佳方法是使用(如@user2503849 所说)TextUtlis.substring(CharSequence, int, int) 方法。我可以解释为什么。如果您查看android.jar(最新API 22)中的String.substring(int, int) 方法,您会看到:

            public String substring(int start) {
                if (start == 0) {
                    return this;
                }
                if (start >= 0 && start <= count) {
                    return new String(offset + start, count - start, value);
                }
                throw indexAndLength(start);
            }
            

            好吧,比...你觉得私有构造函数String(int, int, char[]) 是什么样子的?

            String(int offset, int charCount, char[] chars) {
                this.value = chars;
                this.offset = offset;
                this.count = charCount;
            }
            

            正如我们所见,它一直引用“旧”值char[] 数组。所以,GC 无法释放它。

            在最新的 Java 中已修复:

            String(int offset, int charCount, char[] chars) {
                this.value = Arrays.copyOfRange(chars, offset, offset + charCount);
                this.offset = offset;
                this.count = charCount;
            }
            

            Arrays.copyOfRange(...) 内部使用原生数组复制。

            就是这样:)

            最好的问候!

            【讨论】:

              【解决方案11】:

              所有响应者都给出了很好的答案。但是,我为您提供了所有相关的方法,以便任何人都可以从一个地方获得,如果我发现新的东西,我会编辑我的答案。

              1)substring(0)- 用于从给定的特定字符中剪切字符串。

              2)Substring(0,2)- 给你从开始(0)和结束(2)字符开始的子字符串。

              3)Split("NAME")-首先将字符串分为两部分返回,即您在拆分“NAME”中使用,另一部分是字符串组合的其余部分。

              4)subSequence(0,3) - 返回给定开始(0)和结束索引(3)的序列。

              *这不是专门用于分割字符串,但它可能会被完全用于某些人

              5)startswith("A",3)- 返回特定起始字符的字符串。

              例如,

              String s = "StackOverflow";
              String[] split = s.split("Stack");
                          System.out.println("STRING NAME:"+s.substring(2));
                          System.out.println("STRING NAME:"+s.substring(2,3));
                          System.out.println("STRING NAME:"+split[1]);
                          System.out.println("STRING NAME:"+split[0]);
                          System.out.println("STRING NAME:"+s.subSequence(2,5));
              

              输出: 1)确认溢出

              2)a

              3)溢出

              4)堆栈

              5)确认

              我希望这将为您提供所需的足够信息。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-09-29
                • 2021-12-23
                • 1970-01-01
                • 2012-02-28
                • 2011-11-17
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多