对java不熟,简单的问题请教!

cenphoenix 2004-06-16 05:51:50
我想问一下java中有没有判断字符串关系的函数。
比如
1) 两个字符串str1和str2,想判断str1是否是str2的子串,用哪个函数(方法)
2) 也是两个字符串str1和str2,想判断str1是否和str2的格式相象,如str1是“abcd”,而str2是“abbd”。是用equalsIgnoreCase(String anotherString) 这个方法吗?不是的话用什么方法。
...全文
152 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
scvzhang 2004-06-18
  • 打赏
  • 举报
回复
1 str2.indexOf(str1)
2 NO
POwner 2004-06-17
  • 打赏
  • 举报
回复
有一部分像
POwner 2004-06-17
  • 打赏
  • 举报
回复
也就是说要判断两字符的公有部分是什么字符
happyegg 2004-06-17
  • 打赏
  • 举报
回复
那我问你一个问题
1)
a123456789b
a!@#$%^&*(b
像不像?
2)
a12345678bb
a!@#$%^&*bb
像不像?
3)
aa2345678bb
aa@#$%^&*bb
像不像?
4)
aaa345678bb
aaa#$%^&*bb
像不像?
5)
aaa34567bbb
aaa#$%^&bbb
像不像?
6)
aaaa4567bbb
aaaa$%^&bbb
像不像?
7)
aaaa456bbbb
aaaa$%&bbbb
像不像?
8)
aaaaa56bbbb
aaaaa%&bbbb
像不像?
9)
aaaaa5bbbbb
aaaaa%bbbbb
像不像?
你能答得出来,那么程序也可以写出来了
HawaiiLeo 2004-06-17
  • 打赏
  • 举报
回复
你个第二个问题好象不能用现成的函数解决啊。
homeness 2004-06-17
  • 打赏
  • 举报
回复
没有这种函数
cenphoenix 2004-06-17
  • 打赏
  • 举报
回复
我的意思是说,比如str1是"aaaabbbbcccc" 而str2是"aaaaddddcccc"他们前后都一样,就是中间不同,不知道有没有函数来判断这种情况呢??
谢谢各位的帮忙!
wandou999 2004-06-17
  • 打赏
  • 举报
回复
indexOf("xxx")
BigBangBug 2004-06-16
  • 打赏
  • 举报
回复
是啊,你的2)条件不清楚啊,不知道你所谓的格式相像是什么意思
equalsIgnoreCase方法是不区分大小写得判断两个字符串是否一样
Arias 2004-06-16
  • 打赏
  • 举报
回复
1) 两个字符串str1和str2,想判断str1是否是str2的子串,用哪个函数(方法)
用str2.indexOf(str1),是的话返回一个整数>=0,不是返回一个值 -1;

2) 也是两个字符串str1和str2,想判断str1是否和str2的格式相象,如str1是“abcd”,而str2是“abbd”。是用equalsIgnoreCase(String anotherString) 这个方法吗?不是的话用什么方法。
字符窜只能判断是不是完全相同或者某些部分相同,而不能说是格式想像。

public boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.
ivy8890 2004-06-16
  • 打赏
  • 举报
回复
你说的“格式相象“太不具体了,无从下手。
你可以提供更详细的,如:
都以字符"a"开头一类的。
上海小旋風 2004-06-16
  • 打赏
  • 举报
回复
不对啊,一个显示-1,一个显示4
discolt 2004-06-16
  • 打赏
  • 举报
回复
1>
str1 是否是 str2的子串
public class Test7 {
public static void main(String[] args) {
String str1 = "AAAA";
String str2 = "BBBB";
String str3 = "CCCCBBBB";
System.out.println(str3.indexOf(str1)); // -1表示不是
System.out.println(str3.indexOf(str2)); // >1表示存在
}

}

2>
str1.length() == str2.length() ?

62,614

社区成员

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

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