美文网首页
String文本里面查找一个字符或者字符串出现了几次

String文本里面查找一个字符或者字符串出现了几次

作者: 皓皓amous | 来源:发表于2023-08-23 16:36 被阅读0次
      String content = "test";
      String word = "t";
      int end = 0;
      List<int[]> indexNumbers = new ArrayList<>();
      getIndex(indexNumbers,content,word,end);
      for (int i = 0; i < indexNumbers.size();i++) {
          int[] ints = indexNumbers.get(i);
          for (int j = 0;j < ints.length;j++) {
              Log.d(TAG,"TEST "+" i " +i + " j"+ints[j]);
          }
      }


  public static void getIndex(List<int[]> indexNumbers, String text, String word, int end) {
      if (indexNumbers == null || TextUtils.isEmpty(text) || TextUtils.isEmpty(word) || end > text.length()) {
          return;
      }
      int index;
      while((index = text.indexOf(word,end)) != -1) {
          end = index + word.length();
          int[] indexNumber = {index, end};
          indexNumbers.add(indexNumber);
      }
  }


下划线转义符:"\""
"\""+key+"\""

相关文章

  • 字符串查找

    从一个完整的字符串之中查找子字符串的存在就属于字符串查找,在String类里面提供如下的查找方法: indexOf...

  • js基础之字符串方法

    indexOf,search查找字符串中指定文本首次出现的索引位置 lastIndexOf查找字符串中指定文本最后...

  • JS String对象

    String 对象用于处理文本(字符串)。String 对象创建方法: new String()。或者更简单方式:...

  • mac下常用终端命令

    1.vim 搜索关键词:/ 后输入查找的字符串,点回车,显示文本中第一个出现的字符串。?后输入查找的字符串,点回车...

  • 字符串常用操作

    字符串查找: 案例: indexOf(String s):返回字符串首次出现的索引位置,有则返回相应索引位置,没有...

  • String 常用方法

    1、查找字符串中的字符串 indexOf() 返回指定文本在字符串首次出现的索引 没有返回-1 last...

  • 10-第十章 字符串方法和数组

    String即文本(字符串),字符串方法都不改原字符串; 创建 字符串 的三种办法: new String(), ...

  • 字符串匹配

    字符串匹配(string matching)也称字符串查找(string searching)或模式匹配(patt...

  • Java 字符串常用操作(String类)

    字符串查找 String提供了两种查找字符串的方法,即indexOf与lastIndexOf方法。 1、index...

  • 【Java基础】Java字符串常用操作(String类)

    1. 字符串查找:indexOf String提供了两种查找字符串的方法,即indexOf与lastIndexOf...

网友评论

      本文标题:String文本里面查找一个字符或者字符串出现了几次

      本文链接:https://www.haomeiwen.com/subject/ftipmdtx.html