简体   繁体   English

使用字符串indexOf java

[英]using string indexOf java

im doing a program where i need to return the index of MyString of the first occurence. 我正在做一个程序,我需要返回第一次出现的MyString的索引。 idk how to use the indexof in this situation. idk在这种情况下如何使用indexof。 i think it might be something like ch = indexof[] but im not sure. 我认为可能是ch = indexof [],但我不确定。 im still new at this and the api didnt help me much 我在这方面仍然很新,并且api对我没有太大帮助

/*********************************************************************************
     This program will create classes that do similar operations to the Java String

     Javier Perez
     csc110
     11/5/12
    *********************************************************************************/
    package string.assignment;
    public class MyString
    {

        private char[] array;
        private int size;
        private int max;

        public MyString()
        {
            array = new char[25];
            max = 25;
        }
        public void setString(String newString)
        {
         if(newString.length() > 25)
         {
             System.out.println("/nEnter a number equal or less than 25 " );

         }
         else
         {
          for(int i=0; i < newString.length(); i++)
          {
            array[i] = newString.charAt(i);
          }
         }
        }


        public String toString()
        {
            return new String(array);
        }

        public char charAt(int index)
        {
            return array[index];
        }

        public boolean contains(char ch)
        {
            for(char c: array)
            {
                if(c == ch) return true;
            }
            return false;
        }
        public int indexOf( char ch )
        {
            ch = ch.indexOf();

        }
        return index; 


    }

For fetching the index of a certain character from your array, you would have to iterate over the array, and compare each character with the one you want to check. 为了从数组中获取某个字符的索引,您必须迭代该数组,并将每个字符与要检查的字符进行比较。 If any character matches, return the index immediately. 如果有任何字符匹配,请立即返回索引。

Also, your last return statement should be inside your method, else your code would not compile. 另外,您的最后一个return语句应该在您的方法内部,否则您的代码将无法编译。

So, your indexOf method in its simplest would be like this: - 因此,最简单的indexOf方法将如下所示:-

public int indexOf( char ch ) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == ch) {
            return i;   // Character found, return current index
        }
    }
    return -1;    // Character not found. Return -1
}

Iterate your array of characters, match the desired character against the character at index in the array. 迭代字符数组,将所需字符与数组索引处的字符匹配。 If match found, not the position in the index variable which has default value as -1 and break the loop. 如果找到匹配项,则没有将index变量的位置(默认值为-1中断循环。 Return the index value from the method. 从方法返回index值。

     public int indexOf( char ch ){
        int index = -1;
        for(int i=0; i < array.length; i++){
         if(array[i] == ch){
             index = i;//first match found, stop searching
             break;
         }
       }
       return index; 
     }

This way, it returns the first matching position of the character in the string's character array, otherwise returns -1 . 这样,它将返回字符串的字符数组中字符的第一个匹配位置 ,否则返回-1

If it does indeed compile then your cut and paste is bad - what is that return index doing at the end? 如果确实可以编译,则您的剪切和粘贴效果很差-最后的return index在做什么?

Anyway - not I've used your own charAt rather than just a char compare. 无论如何-我不是用自己的charAt而不仅仅是使用char比较。

    public int indexOf( char ch )
    {
        int result = -1;
        for(int i = 0; i <  size; i++) {
            if (charAt(i) == ch) {
                result = i;
                break;
            }
        }
        return result;

    }

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

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