方法:
使用String类的indexOf()方法可以判断一个字符串是否在另一个字符串中出现,其方法原型为:
int java.lang.String.indexOf(String arg0)如果字符串arg0出现在源字符串中,返回arg0在源字符串中首次出现的位置。
Java程序:
public class Main {public static void main(String[] args) {
String key = "wo"
char[] arr = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'}
String source= String.valueOf(arr)
if(source.indexOf(key) >= 0) {
System.out.printf("\"%s\" 中包含 \"%s\"", source, key)
}
else {
System.out.printf("\"%s\" 中不包含 \"%s\"", source, key)
}
}
}
运行测试:
"Hello, world!" 中包含 "wo"可以用替换函数来实现删除功能,比如要删除字符“java”,那么只需要用replace函数将java用空白来代替。如下:public static void main (String args[]){
String test = "studyjava"
test=test.replace("java","")
}