String类的charAt(int index)方法

DBRBZYY 2005-10-13 10:06:17
编写一个方法来查找在另一个字符串里的特定字符串。如果字符串存在,则方法必须返回真。例如:isSubString(“cat”, “The cat in the hat.”)是true,而isSubString( “bat”, “The cat in the hat.”)是false。
另外,验证边界条件也要遇到。
 isSubString(“The”, “The cat in the hat,”)是true
 isSubString(“hat.”, “The cat in the hat.”)是true。
提示-可以使用String类的charAt(int index)方法找到某个字符串中的特定字符;index是从0开始。例如:“cat”.charAt(0)是‘c’、 “cat”.charAt(1)是 ‘a’、 “cat”.charAt(2)是 ‘t’。length方法返回字符串中的字符数目:例如:“cat”.length()是3。

请那位大哥帮忙解决一下!
...全文
252 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
interhanchi 2005-10-14
  • 打赏
  • 举报
回复
contains
public boolean contains(CharSequence s)
Returns true if and only if this string contains the specified sequence of char values.

Parameters:
s - the sequence to search for
Returns:
true if this string contains s, false otherwise
Throws:
NullPointerException - if s is null
Since:
1.5


public class WORK{
public static void main(String a[]){
//StringBuffer sbuf = new StringBuffer();
String s="The cat in the hat";
String s1 = "The";
System.out.println(s.contains(s1));
}
}
sqlink 2005-10-14
  • 打赏
  • 举报
回复
if("The cat in the hat".indexOf("cat")!=-1){
//is substring
}
Mark_Chen 2005-10-14
  • 打赏
  • 举报
回复
顶!!

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧