简体   繁体   English

在不使用 indexOf 方法的情况下查找字符串的 indexOf

[英]Finding the indexOf a string without using indexOf method

Working on a little java program currently, my task is to understand more on how the indexOf method works without actually using the indexOf method.目前正在处理一个小 Java 程序,我的任务是在不实际使用 indexOf 方法的情况下更多地了解 indexOf 方法的工作原理。 I feel I am close having the code correct.我觉得我已经接近正确的代码了。 I'll provide my code and my test file.我将提供我的代码和我的测试文件。 I want to only use the charAt(), length(), and equals() methods for my program.我只想在我的程序中使用 charAt()、length() 和 equals() 方法。

public class MiniString
{
   private String str;


   public MiniString(String x)
   {
      this.str = x;
   }


   public int findIndexOf(int a)
   {
      int counter = 0;
      for(int i = 0; i < this.str.length(); i++)
      {
         counter++;
      } 

      char[] arr = new char[counter];

      for(int i = 0; i < arr.length; i++)
      {
         arr[i] = this.str.charAt(i);
      }
   }
}

Here is what I am passing into my method这是我传递给我的方法的内容

public class TestMiniString
{
   public static void main(String[] args)
   {

      MiniString s1 = new MiniString("Hello world, welcome to the Java world.");



      System.out.println(s1.findIndexOf('w'));
      System.out.println(s1.findIndexOf('m'));
      System.out.println(s1.findIndexOf('x'));
      System.out.println(s1.findIndexOf('J'));
      System.out.println(s1.findIndexOf('a'));
      System.out.println();

}

It is quite simple.这很简单。 String in Java is a array of the chars internally. Java中的String在内部是一个字符数组。 Using charAt(int index) you have direct access to this.使用charAt(int index)您可以直接访问它。 All you have to is just loop this array and find first occurrence of the required character:你所要做的就是循环这个数组并找到所需字符的第一次出现:

public final class MiniString {
    private final String str;

    public MiniString(String str) {
        this.str = str;
    }

    public int indexOf(int ch) {
        for (int pos = 0; pos < str.length(); pos++)
            if (str.charAt(pos) == ch)
                return pos;

        return -1;
    }

    public int indexOf(String str) {
        if (str == null || str.trim().isEmpty())
            return -1;

        for (int pos = 0; pos < this.str.length() - str.length(); pos++) {
            for (int i = 0; i <= str.length(); i++) {
                if (i == str.length())
                    return pos;
                if (this.str.charAt(pos + i) != str.charAt(i))
                    break;
            }
        }

        return -1;
    }
}

But be ware of exotic symbols, that is codded with more than one character (eg Chinese characters).但要小心异国情调的符号,即编码有多个字符(例如汉字)。 So this is not universal solution for all situations.所以这不是适用于所有情况的通用解决方案。

Look at Java charAt used with characters that have two code units for more details.有关更多详细信息,请查看与具有两个代码单元的字符一起使用的 Java charAt

indexOf(String str) implementation is strigtforward. indexOf(String str)实现是严格的。 Thare're many special algorithms for String searching algorithm .字符串搜索算法有很多特殊的算法

Please try this.请试试这个。 Hopefully, you like it.希望你喜欢。

    static int indexOf(String string, String substring ) {

    String crt = "";
    for (int i = 0; i < string.length(); i++) {

        if (string.charAt(i) == ' ')
            crt = "";
        else
            crt += string.charAt(i);
        if (crt.equalsIgnoreCase(substring))
            return i - crt.length() + 1;
    }
    return -1; 
    }
public static int subStringIndex(String str, String substr) {
        int strlen = str.length();
        int substrlen = substr.length();
       int count=0;
       int substrlen2=substr.length();
        if (substrlen >= 1) {
            for(int i=0;i<strlen-substrlen+1;i++){
                String A=str.substring(i,substrlen2);
                if(A.equalsIgnoreCase(substr)){
                return count+1;
                }else{
                    count++;
                    substrlen2++;
                }
            }
            return -1;              
        }
        return -1;
    }

Find the word index from main string also find which is main string and which is sub string从主字符串中查找单词索引还可以找到哪个是主字符串,哪个是子字符串

public static void main(String[] args) {公共静态无效主(字符串 [] args){

    String string2="Today is a great day";
    String string1="great day";
    int a=string1.length();
    int b=string2.length();
    String [] p=string1.split(" ");
    String [] q=string2.split(" ");
    if(a>b)
    {
        for(int i=0;i<q.length;i++)
        {
            String x=q[i];
            
            char d=x.charAt(0);
            for(int k=0;k<string1.length();k++)
            {
                if(string1.charAt(k)==d)
                {
                     if(string1.charAt(k-1)== ' ')
                 System.out.println(k);
                 
                 
                }
                 
            }
        }
    }
    else
    {
        
        for(int j=0;j<p.length;j++)
        {
            String x=p[j];
            char d=x.charAt(0);
            for(int n=0;n<string2.length();n++)
            {
                if(string2.charAt(n)==d)
                {
                    if(string2.charAt(n-1)== ' ')
                System.out.println(n);
                
                }
            }
        }
    }
    
    
     

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM