【问题标题】:Searching an String array for a sub-string?在字符串数组中搜索子字符串?
【发布时间】:2011-07-04 21:46:03
【问题描述】:

我正在尝试在 java 中编写一个小方法,但我无法弄清楚。我想要做的是输入一个字符串,然后将 int 变量的值设置为数组中 this 的索引,即如果我有一个由

组成的数组
[0] 'hi guys'
[1] 'this'
[2] 'is'
[3] 'sparta'

我的整数的值设置为0,我想找到“ta”的第一次出现,这将是[3],所以我希望函数将我的整数设置为3。

我目前所拥有的完全是错误的,有什么简单的方法可以做到这一点吗?我已经定义了一个名为 get() 的函数,它返回当前行的值(即 get(0) 在这种情况下将返回“嗨,伙计们”)。谁能帮帮我?

非常感谢:)

 public void find(String line ) {
   boolean found = false;
   int i = cursor + 1;
   while ( found = false && i!=cursor) {
   if ((doc.get(cursor).indexOf( line ) > 0)){
  cursor = i;
  found = true;
   }else {
    cursor++;
    cursor%=doc.size();
    i++;

   }
 }
 }

【问题讨论】:

  • 这是作业吗?如果是这样,你应该这样标记它。
  • @steveom:你肯定是在杂草丛中,但为了帮助你,我们需要一些信息:你想放入数组和搜索的文本数据来自哪里? doc 现在是什么类型的对象?您是否正在尝试读取文件?
  • @steveom:还有一些明显的错误和遗漏:found = false 应该是 found == false 你现在分配它而不是比较。我也看不到您实际上正在搜索子字符串的任何地方...从您的示例中:`找到“ta”的第一次出现`
  • @Paul Sasik,修复了 equals 问题。 doc 是我定义的类的一个瞬间,具有数组的所有基本功能。

标签: java arrays string search indexof


【解决方案1】:

通常我不这样做,但今天是星期六,我很高兴,可能会喝醉

public void find(String line ) {
   boolean found = false;
   int i = 0;;
   while (i < doc.size()) {
     if ((doc.get(i).indexOf( line ) > 0)){
       cursor = i;
       found = true;
       break;
     }else {
       i++;
     }
   }
   if (found) {
      // print cursor or do whatever
   }
 }

【讨论】:

    【解决方案2】:

    你应该注意这是否是家庭作业。

    一种方法是:

        int i = 0;
        String searchTerm = "ta";
    
        System.out.println("Following substrings contain search term:");
        for (String s : "hi guys,this,is,sparta".split(",")) {
            if (s.contains(searchTerm)) System.out.println(i++);
            else i++;
        }
    

    或者,如果您更喜欢使用正则表达式,请将 s.contains(searchTerm) 更改为 s.matches(searchTerm)

    如果这不是家庭作业,而是面试问题或工作问题,这将复杂得多。例如:氨基酸序列是搜索词,需要在DNA/RNA中找到它所在的位置。在这种情况下,您需要更复杂的解决方案。

    例子:

    【讨论】:

      【解决方案3】:

      如果正确理解你的任务,我会做这样的事情:

      public int find(String line, int startPosition) {
          if (doc[startPosition].contains(line) {
              return startPosition;
          }
          for (int i = 0; i < Math.max(doc.size() - startPosition, startPosition); i++) {
              if (startPosition - i > 0 && doc[startPosition - i].contains(line)) {
                  return startPosition - i;
              }
              if (startPosition + i < doc.size() && doc[startPosition + i].contains(line)) {
                  return startPosition + i;
              }
      
          }
          return -1;
      }
      

      这将返回数组中包含作为行参数传递的子字符串的第一个元素的索引。

      【讨论】:

      • 这可能非常接近,但我认为 OP 需要在每一行上进行子字符串搜索:来自问题 find the first occurrence of "ta" 这是第三个示例条目的子字符串。
      • 是的,这是正确的,保罗。我需要在每一行中搜索子字符串。我遇到的主要问题是我有一个值,我们称它为 i,它指向数组,并且可能指向数组中间的一个位置,我想遍历数组并找到最近的i 的索引,到子字符串所在的数组中。抱歉,我之前没说清楚!
      • 好的,我上面实现的确实搜索了一个子字符串,所以我不确定 Paul 在说什么。现在它确实返回数组中的 first 索引,该索引对应于包含作为参数传递的子字符串的字符串,而似乎应该有另一个参数,并且索引应该是最接近的那个参数值。那样的话,算法需要调整,我会适当地编辑我的原始代码。
      • 非常感谢 Alexander,我已经在课堂上运行了这段代码,它比我以前做的要干净得多,它只需要一点适应 :) 谢谢 :)
      【解决方案4】:

      他说这不是家庭作业,所以这里是:

      (这实际上可以编译和工作)

          import java.io.*;
      
          public class A {
                  public static void main(String[] args) {
                          String[] arr = {"hi guys", "this", "is", "sparta"};
                          System.out.println("enter substring:");
                          String substr = "";
                          try {
                          substr = new BufferedReader(new InputStreamReader(System.in)).readLine();
                          } catch(IOException e) {System.exit(0);}
                          for(int i =0; i<arr.length; i++) {
                                  int charPos = arr[i].indexOf(substr);
                                  if(charPos!=-1) {
                                          System.out.println("found in string index " + i + " at "+charPos);
                                          break;
                                  }
                          }
                  }
          }
      

      【讨论】:

        【解决方案5】:

        在实际的字符串[]而不是每一行中搜索不是更明智吗?

        如果该位置的字符串包含子字符串,则循环遍历数组并返回当前索引。

        【讨论】:

          猜你喜欢
          • 2022-01-23
          • 2013-01-09
          • 2019-10-06
          • 1970-01-01
          • 2015-05-12
          • 1970-01-01
          • 2013-05-25
          • 2018-11-28
          相关资源
          最近更新 更多