Java-将字符串与字符串数组进行匹配
例如,我有一个字符串数组Java-将字符串与字符串数组进行匹配,java,arrays,regex,string,Java,Arrays,Regex,String,例如,我有一个字符串数组 String[]arr=[“问题”、“这”、“是”、“a”] 我有一个字符串,例如string q=“a foo This bar is This foo question a bar question foo”(我知道是人为的例子) 对于我来说,最好的方法是将arr与q进行匹配,并打印出所有出现的arr[i],但不按顺序打印?因为每次我尝试这样做时,它都会按照原始数组在arr中出现的顺序返回原始数组,而不是按照它们出现的顺序返回所有出现的数组 简单地说,我希望我的结果
String[]arr=[“问题”、“这”、“是”、“a”]代码>
我有一个字符串,例如string q=“a foo This bar is This foo question a bar question foo”代码>(我知道是人为的例子)
对于我来说,最好的方法是将arr
与q
进行匹配,并打印出所有出现的arr[i]
,但不按顺序打印?因为每次我尝试这样做时,它都会按照原始数组在arr
中出现的顺序返回原始数组,而不是按照它们出现的顺序返回所有出现的数组
简单地说,我希望我的结果类似于[“a”,“This”,“is”,“This”,“question”,“a”,“question”]
,而我只是得到原始数组
我的代码:
public static void ParseString(String[] arr, String q) {
for (int i = 0; i < arr.length; i++) {
if (q.contains(arr[i])) {
System.out.println(arr[i]);
}
}
}
publicstaticvoidparsestring(String[]arr,String q){
对于(int i=0;i
我意识到这可能是一个非常明显的错误,因此提前感谢您的耐心。不要在数组上循环,而要在字符串上循环,就像
String q = "a foo This bar is This foo question a bar question foo";
String[] arr = {"question", "This", "is", "a"};
List<String> list = Arrays.asList(arr);
for(String s:q.split(" ")){
if(list.contains(s)){
System.out.println(s);
}
}
String q=“a foo此栏是此foo问题a bar问题foo”;
字符串[]arr={“问题”、“这”、“是”、“a”};
List=Arrays.asList(arr);
用于(字符串s:q.split(“”){
如果(列表包含){
系统输出打印项次;
}
}
您本可以避免使用列表
,并在数组上循环,但我发现这样代码更清晰。不要在数组上循环,在字符串上循环,如
String q = "a foo This bar is This foo question a bar question foo";
String[] arr = {"question", "This", "is", "a"};
List<String> list = Arrays.asList(arr);
for(String s:q.split(" ")){
if(list.contains(s)){
System.out.println(s);
}
}
String q=“a foo此栏是此foo问题a bar问题foo”;
字符串[]arr={“问题”、“这”、“是”、“a”};
List=Arrays.asList(arr);
用于(字符串s:q.split(“”){
如果(列表包含){
系统输出打印项次;
}
}
您本可以避免使用列表
,并在数组上循环,但我发现这样代码更清晰。您可以将字符串拆分为每个单词的数组,然后在字符串数组中循环每个单词
String[] arr = {"question", "This", "is", "a"};
String q = "a foo This bar is This foo question a bar question foo";
String[] splitString = q.split(" ");
for (String wordString: splitString) {
for (String wordArray : arr) {
if (wordString.equalsIgnoreCase(wordArray)) {
System.out.println(wordArray);
}
}
}
可以将字符串拆分为每个单词的数组,然后循环遍历字符串数组中的每个单词
String[] arr = {"question", "This", "is", "a"};
String q = "a foo This bar is This foo question a bar question foo";
String[] splitString = q.split(" ");
for (String wordString: splitString) {
for (String wordArray : arr) {
if (wordString.equalsIgnoreCase(wordArray)) {
System.out.println(wordArray);
}
}
}
(1)计算发生次数(2)打印结果如何
public void countWords() {
String[] queries = { "question", "This", "is", "a" };
String data = "a foo This bar is This foo question a bar question foo";
//prepare index
Map<String, Integer> index= new HashMap<>();
for (String w : data.split(" ")) {
Integer count=index.get(w);
if(count==null){
index.put(w, 1);
}else{
index.put(w, count+=1);
}
}
//query index
for(String w:queries){
int i=index.get(w);
System.out.println(String.format("%d\t%s", i,w));
}
}
(1)计算发生次数(2)打印结果如何
public void countWords() {
String[] queries = { "question", "This", "is", "a" };
String data = "a foo This bar is This foo question a bar question foo";
//prepare index
Map<String, Integer> index= new HashMap<>();
for (String w : data.split(" ")) {
Integer count=index.get(w);
if(count==null){
index.put(w, 1);
}else{
index.put(w, count+=1);
}
}
//query index
for(String w:queries){
int i=index.get(w);
System.out.println(String.format("%d\t%s", i,w));
}
}
只需使用正则表达式。默认情况下,正则表达式从左到右检查字符串。然后,找到的所有匹配项在目标字符串中按出现顺序排列。所以用array=>做一个正则表达式(问题|这|是| a)
。如果您有一个非常大的静态数组,请使用生成一个正则表达式三元树字符串。将它复制到源代码中,然后在运行时使用它构造一个正则表达式对象。例如,只需使用正则表达式。默认情况下,正则表达式从左到右检查字符串。然后,找到的所有匹配项在目标字符串中按出现顺序排列。所以用array=>做一个正则表达式(问题|这|是| a)
。如果您有一个非常大的静态数组,请使用生成一个正则表达式三元树字符串。将它复制到源代码中,然后在运行时使用它构造一个正则表达式对象。例如a