-- 急用 加分--求个JAVA题代码

来源:百度知道 编辑:UC知道 时间:2024/05/26 06:57:19
创建两个String类,有 对象str1和str2,判断str2是否是str1的子串。如果是输出str1中在子串前和后的字符串。如:“Action”是“addActionListener”的子串,在此子串前是字符串“add”,后面是字符串“Listener”

我昨天才看到 String 中的 方法 split()

class Test{
public static void main(String []args){
String str1 = new String("addActionListener");
String str2 = new String("Action");
if(str1.indexOf(str2)!=-1){
String []s = str1.split("Action");
System.out.println(s[0]);
System.out.println(s[1]);
}
}
}

都是用的 String 中的方法,这个类很强大的!
多看看 API !