package com;
public class Mycode {
public static void main(String[] args) {
System.out.println(prefixAgain("nishikantnishikant", 11));//false - if you have repeated string it will return false till index is greater than length of the string
System.out.println(prefixAgain("nishikant", 3));//false -if there is no repeat, you can get false
System.out.println(prefixAgain("nishikant", 2));//true
System.out.println(prefixAgain("nishikantnishikant", 10));//true - if you have repeated string it will return true till index is equal to length of the string
}
public static boolean prefixAgain(String str, int n) {
String prefix = str.substring(0,n-1);
int index = str.indexOf(prefix,n-1);
if (index != -1)
{return true;}
else
{return false;}
}
}
你想用这段代码实现什么? – Einar
要“真”吗?我不希望从这个代码。请制作一个MCVE。该代码找到字符串的第一个“n-1”字符的第一个索引,但是从索引“n-1”开始。本质上,如果它返回'true',那么在字符串中第一个'n-1'字符会相互重复。但是,这是在这里的意图? – Tunaki
请注意,您的if/else块等同于'return index!= -1;',这可能更具可读性。 – assylias