首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在不使用indexOf内置方法Java的情况下实现indexOf和lastIndexOf?

如何在不使用indexOf内置方法Java的情况下实现indexOf和lastIndexOf?
EN

Stack Overflow用户
提问于 2018-10-23 00:00:21
回答 1查看 0关注 0票数 0

在我的赋值中,我们应该重新创建indexOf和lastIndexOf,然后将字符串2与字符串1进行比较,以查看字符串2是否在第一个字符串内。但是,即使字符串2不在字符串1中,它仍然显示字符串2在其中的某个位置,当它正确运行时,它似乎向后读取字符串1并从那里给我答案

`import java.util.Scanner;
class MyString {
  String s;

MyString() {
    s = "";
}

MyString(String str) {
    s = str;
}

void setMyString (String str) {
    s = str;
}

String getMyString() {
    return s;
}

//return the index within this.s of the first occurrence of s. 
//return -1 when s is not found on this.s.
//if this.s = "aabababb", s = "aba", return 1
//if this.s = "aabababb", s = "abaa", return -1
int indexOf(String s) {
    int i=0;
    for(i = 0; i < this.s.length()-s.length(); i++) {
        for (int j = 0; j < s.length(); j++) {
            if(this.s.charAt(i+j) == s.charAt(j)) {
                i++;
            }
            else
                i=-1;
            break;
        }
    }       
    return i; //going from end of string to beginning, needs fixed
}

//return the index within this.s of the last occurrence of s.
//return -1 when s is not found on this.s.
//if this.s = "aabababb", s = "aba", return 3
 //if this.s = "aabababb", s = "abaa", return -1
// int lastIndexOf(String s) {
 //   for (int i=this.s.length(); i >=0; i--) {
   //     for (int j = s.length(); j>= 0; j--) {

}

public class Lab8 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    MyString myS = new MyString();

    String s1, s2;// inputs

    System.out.print("Enter string 1:");
    s1 = in.nextLine();
    System.out.print("Enter string 2:");
    s2 = in.nextLine();

    myS.setMyString(s1);

    int p = myS.indexOf(s2);
    if(p==-1)
        System.out.println(s2+" not found on "+myS.getMyString());
    else
        System.out.println(s2+" found at "+p+" on "+myS.getMyString());

    p = myS.lastIndexOf(s2);
    if (p == -1)
        System.out.println(s2+" not found on "+myS.getMyString());
    else
        System.out.println(s2+" found at "+p+" on "+myS.getMyString());
}

}`

EN

回答 1

Stack Overflow用户

发布于 2018-10-23 09:26:18

我不知道你是否被允许使用子字符串来验证匹配,因此我创建了一个循环,看起来每个字符都是一个接一个匹配。我会改变你的方法,以便使用这些if语句和for循环:

int indexOf(String s) {
    int result = -1;
    int i, j;
    for(i = 0; i < this.s.length(); i++) {
        if(this.s.charAt(i) == s.charAt(0)) {
            for(j = 1; j < s.length(); j++) {
                if(this.s.charAt(i+j) != s.charAt(j)){
                    break;
                } else if(j == s.length() - 1) {
                    result = i;
                }
            }
        }
    }
    return result; //going from end of string to beginning, needs fixed
}

输出继电器:

Enter string 1: wfqe fwef wefty eqwr ew
Enter string 2:ty
14

第二输出:

Enter string 1:w rq rwerwerwerwer ewrwerwerwe
Enter string 2:hello
-1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档