Java 返回介于第一次出现和最后一次出现之间的字符串";“面包”;在给定字符串中

Java 返回介于第一次出现和最后一次出现之间的字符串";“面包”;在给定字符串中,java,Java,这个问题来自编码bat,以前可能有人问过。请检查代码 三明治是两片面包中间夹着东西。返回给定字符串中第一次和最后一次出现“bread”之间的字符串,如果没有两片bread,则返回空字符串“” getSandwich("breadjambread") → "jam" getSandwich("xxbreadjambreadyy") → "jam" getSandwich("xxbreadyy") → "" 我的代码是 public static String getSandwich(String

这个问题来自编码bat,以前可能有人问过。请检查代码

三明治是两片面包中间夹着东西。返回给定字符串中第一次和最后一次出现“bread”之间的字符串,如果没有两片bread,则返回空字符串“”

getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""
我的代码是

public static String getSandwich(String str) {
        int ind = str.indexOf("bread");
        int laind = str.lastIndexOf("bread");
        if(!(laind == -1 ))return (str.substring(ind+5,laind)) ;
        return "";
    }
我越来越

Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -5 (line number:4)
对于此输入,请参见以下代码

public static String getSandwich(String str) {
    int ind = str.indexOf("bread");
    int laind = str.lastIndexOf("bread");
    if((laind != -1 ) && (ind!=laind))
        return (str.substring(ind+5,laind)) ;
    return "";
}
我将
(ind!=laind)
添加到检查第一个和最后一个“面包”是否不同的条件中

请参阅此处。

请参阅以下代码

public static String getSandwich(String str) {
    int ind = str.indexOf("bread");
    int laind = str.lastIndexOf("bread");
    if((laind != -1 ) && (ind!=laind))
        return (str.substring(ind+5,laind)) ;
    return "";
}
public class Main {
    public static void main(String aregs[]) {
        String word = "xxbreadyy";
        System.out.println(get(word));
    }

    public static String get(String word) {
        int firstAccurance = getPosition(word);
        System.out.println("first = " + firstAccurance);
        if (firstAccurance != -1 && firstAccurance + 5 < word.length()) {
            String newWord = word.substring(firstAccurance + 5);
            System.out.println("newWord = " + newWord);
            int secondAccurance = getPosition(newWord);
            System.out.println("second = " + secondAccurance);
            if (secondAccurance != -1) {
                return word.substring(firstAccurance+5, secondAccurance+firstAccurance+5);
            } else
                return "";
        } else
            return "";
    }

    private static int getPosition(String word) {
        int i = word.indexOf("bread");
        return i;
    }
}
我将
(ind!=laind)
添加到检查第一个和最后一个“面包”是否不同的条件中

请参见此处。

public class Main{
public class Main {
    public static void main(String aregs[]) {
        String word = "xxbreadyy";
        System.out.println(get(word));
    }

    public static String get(String word) {
        int firstAccurance = getPosition(word);
        System.out.println("first = " + firstAccurance);
        if (firstAccurance != -1 && firstAccurance + 5 < word.length()) {
            String newWord = word.substring(firstAccurance + 5);
            System.out.println("newWord = " + newWord);
            int secondAccurance = getPosition(newWord);
            System.out.println("second = " + secondAccurance);
            if (secondAccurance != -1) {
                return word.substring(firstAccurance+5, secondAccurance+firstAccurance+5);
            } else
                return "";
        } else
            return "";
    }

    private static int getPosition(String word) {
        int i = word.indexOf("bread");
        return i;
    }
}
公共静态void main(字符串aregs[]){ String word=“xxbready”; System.out.println(get(word)); } 公共静态字符串get(字符串字){ int firstAccurance=getPosition(字); System.out.println(“first=“+firstAccurance”); if(firstAccurance!=-1&&firstAccurance+5
公共类主{
公共静态void main(字符串aregs[]){
String word=“xxbready”;
System.out.println(get(word));
}
公共静态字符串get(字符串字){
int firstAccurance=getPosition(字);
System.out.println(“first=“+firstAccurance”);
if(firstAccurance!=-1&&firstAccurance+5
它失败,因为子字符串方法的开始索引参数不应大于结束索引。在您的例子中,它分别是7和2。

它失败了,因为子字符串方法的开始索引参数不应该大于结束索引。在您的例子中,它分别是7和2。

publicstringgetsandwich(stringstr)
public String getSandwich(String str) 
{
  int l = str.length();
  int[] b;
  int count=0;
  b=new int[2];
  if (l<10) return "";
  for (int i=0;i<l-4;i++)
    {
    if (str.substring(i,i+5).equals("bread")) 
        {
        if (count==2) count=1;
        b[count]=i;
        count++;
        }    
    }
  if (count==2)
  {
  return str.substring(b[0]+5,b[1]);
  }  
  return "";
}
{ int l=str.length(); int[]b; 整数计数=0; b=新整数[2]; if(l
publicstringgetsandwich(stringstr)
{
int l=str.length();
int[]b;
整数计数=0;
b=新整数[2];
如果(lGosh)为什么有这么多行

public String getSandwich(String str) {
    if (str.length() <= 10) return "";
    return str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread"));
}
publicstringgetsandwich(stringstr){
如果(str.length()天哪,为什么有这么多行

public String getSandwich(String str) {
    if (str.length() <= 10) return "";
    return str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread"));
}
publicstringgetsandwich(stringstr){
if(str.length()
publicstringgetsandwich(stringstr)
{
//如果我们有“面包”+x+“面包”所需的最小字符数,
//其中x是一个非空字符串。
如果(str.length()>=11)
{
字符串toTest=“面包”;
字符串ret=“”;
int计数器=0;
int-wordStart=0;
int-wordEnd=0;
//向前测试第一个“面包”实例。
对于(int i=5;i
公共字符串getSandwich(字符串str)
{
//如果我们有“面包”+x+“面包”所需的最小字符数,
//其中x是一个非空字符串。
如果(str.length()>=11)
{
字符串toTest=“面包”;
字符串ret=“”;
int计数器=0;
int-wordStart=0;
int-wordEnd=0;
//向前测试第一个“面包”实例。
对于(int i=5;ipublic String getSandwich(String str) {
  int bread1= str.indexOf("bread");
  int bread2= 0;
  for(int i=str.length()-1;i>=0;i--){
    if(str.charAt(i)=='d'&&str.charAt(i-1)=='a'&&str.charAt(i-2)=='e'&&str.charAt(i-3)=='r'&&str.charAt(i-4)=='b'){
      bread2 = i-4;
      if(bread1!=bread2)
        return str.substring(bread1+5,bread2);
    }
  }
    return "";  
}
 public String getSandwich(String str) {
    String bread = "bread";

     int lIndex = str.indexOf(bread);
     int rIndex = str.lastIndexOf(bread);

      if(lIndex != -1 && rIndex != lIndex){
          return str.substring(lIndex + bread.length(), rIndex);
      }
      return "";
   }
public String getSandwich(String str) {
    final String BREAD = "bread";
    if(str.length() < BREAD.length() * 2){
        return "";
    }
    if(str.substring(0, BREAD.length()).equals(BREAD) && 
        str.substring(str.length() - BREAD.length()).equals(BREAD)){
        return str.substring(BREAD.length(), str.length() - BREAD.length());
    }
    if(! str.substring(0, BREAD.length()).equals(BREAD)){
        return getSandwich(str.substring(1));
    }
    return getSandwich(str.substring(0, str.length() - 1));
}
public String getSandwich(String str) {
    return  str.matches("(.*bread.*){2}") ? str.substring(str.indexOf("bread")+5,str.lastIndexOf("bread")) : "";    
}
public String getSandwich(String str) {

  int a = str.indexOf("bread");

  int b = str.lastIndexOf("bread");

  return b-a> 4 ?str.substring(a+5,b): "";

}