这不是关于过滤坏词所以进入。想象一下,你有一个名为的字符串列表 abcd,abcde,abcdef,abfg,abdc如果用户在ConsoleProgram abcd中给出字符串abc作为过滤器,abcde和abcdef将被打印出来。我想过使用子串但我无法实现它有没有人有任何想法.. 请注意,我是java的新手并不称道!
答案 0 :(得分:3)
您需要的第一件事是了解String Regex,可能在这里:http://docs.oracle.com/javase/tutorial/essential/regex/。 试试下一个简单的代码。
public class StringMatcher {
public static void main(String[] args) {
String[] words = new String[]{"abcd", "abcde", "abcdef", "abfg", "abdc"};
String filter = "abc";
for (String word : words) {
if (word.matches(filter + "(.*)")) {
System.out.println("This pass the filter: " + word);
}
}
}
}
答案 1 :(得分:1)
试试这个:如果列表中的项目包含用户输入的字符串,它将被打印。
input = "abc";
for(int i = 0 ; i < list.size(); i++){
if (list.get(i).contains(input))
System.out.println(list.get(i));
}
答案 2 :(得分:1)
假设您的字符串位于名为wordBank的ArrayList中。
for(String i : wordBank) { //loops through the array with 'i' being current index
if(i.contains("abc")) { //checks if index contains filter String
System.out.println(i); //prints out index if filter String is present
}
}
答案 3 :(得分:0)
使用String.contains()
- 当字符串包含给定的char序列时返回true,请参阅string-javadoc