2014-06-04 108 views
-1

给定一个字符串,如果字符串以“hi”开头则返回true,否则返回false。示例输出:字符串越界异常

startHi("hi there") = true 
startHi("hi") =true 
startHi("hello hi") =false 

实际代码:

public boolean startHi(String str) { 
    String firstTwo = str.substring(0,2); 

if (str.length() < 2) return false; 

    if (firstTwo.equals("hi")) { 
    return true; 
    } else { 


    return false; 
    } 
} 

一切运行,所不同的是小于2个字符的字符串。它一直给的误差:

异常:java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:2(行号:2)

+1

这是功课吗?如果没有,使用一行'return str.startsWith(“hi”);' –

+0

codingbat dot com – king

+0

我希望每个人都停止downvoting我的问题所有的时间..这是somethign我不明白,我道歉它也是基本 – king

回答

2

您需要将substring电话后面的长度检查!

public boolean startHi(String str) { 

    if (str.length() < 2) return false; 

    String firstTwo = str.substring(0,2); 
    if (firstTwo.equals("hi")) { 
    return true; 
    } else { 
    return false; 
    } 
} 

substring调用否则失败,因为它 - 作为异常告诉我们 - 需要长度为2(或更长)的字符串。

整个方法可能是更复杂,因为Java字符串有startsWith方法:

public boolean startHi(String str) { 
    return str.startsWith("hi"); 
} 
1

尝试反转方法的第一2个字符串。

public boolean startHi(String str) { 
    if (str.length() < 2) return false; 

    String firstTwo = str.substring(0,2); 
    .... 
} 

否则,如果你离开排在首位的substring线,你已经假定该字符串长度至少为2个字符。如果情况不是这样,它会引起异常。将长度验证放在第一位可以保证字符串的最小长度。

Beyind,你可以尝试使用字符串方法startsWith来代替。

0

试试这个:

public boolean startHi(String str) { 
    if (str.length() < 2) return false; 
    else{ 
     String firstTwo = str.substring(0,2); 
      if (firstTwo.equals("hi")) { 
      return true; 
      } 
       else { 
       return false; 
       } 
      } 
    } 
0

项记载也可以考虑使用String c的startsWith方法姑娘。它的实现可以通过sun jdk源代码获得。

/** 
    * Tests if the substring of this string beginning at the 
    * specified index starts with the specified prefix. 
    * 
    * @param prefix the prefix. 
    * @param toffset where to begin looking in this string. 
    * @return {@code true} if the character sequence represented by the 
    *   argument is a prefix of the substring of this object starting 
    *   at index {@code toffset}; {@code false} otherwise. 
    *   The result is {@code false} if {@code toffset} is 
    *   negative or greater than the length of this 
    *   {@code String} object; otherwise the result is the same 
    *   as the result of the expression 
    *   <pre> 
    *   this.substring(toffset).startsWith(prefix) 
    *   </pre> 
    */ 
    public boolean startsWith(String prefix, int toffset) { 
     char ta[] = value; 
     int to = toffset; 
     char pa[] = prefix.value; 
     int po = 0; 
     int pc = prefix.value.length; 
     // Note: toffset might be near -1>>>1. 
     if ((toffset < 0) || (toffset > value.length - pc)) { 
      return false; 
     } 
     while (--pc >= 0) { 
      if (ta[to++] != pa[po++]) { 
       return false; 
      } 
     } 
     return true; 
    } 



public boolean startsWith(String prefix) { 
     return startsWith(prefix, 0); 
    } 
0

最简单的方法是使用字符串的startsWith方法,但需要首先检查它是否为null。

public boolean startsWithHi(String str) { 
    if (str == null) { 
    return false; 
    } 
    return str.startsWith("hi"); 
} 

在情况下开始 “HI”,还需要返回true一个字符串,你应该做str.toLowerCase()startsWith( “HI”)。