JAVA:ANDROID:字符串的字索引

时间:2018-02-08 13:00:38

标签: java android string indexof

public void searchSong(String songToSearch){
    Toast.makeText(getApplicationContext(), songToSearch, Toast.LENGTH_SHORT).show();
    boolean found = false;
    for(int i = 0; i < fileArray.length; i++ ){
        String value = fileArray[i].toLowerCase().replace(".mp3","");
        if (songToSearch.toLowerCase().matches(value) && !found) {
            Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();
            found = true;
        } else {
            found = false;
        }
        if(found){
            Toast.makeText(getApplicationContext(), "Found", Toast.LENGTH_SHORT).show();
        }
    }
}

我想使用代码来检查songToSearch是否是fileArray中任何字符串的一部分 示例假设fileArray [2]等于“Hello world”并且songToSearch等于“world”我想使boolean找到true ...但是这不会发生...布尔永远不会改变

2 个答案:

答案 0 :(得分:0)

请参阅文档:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

public boolean  matches(String regex)

判断此字符串是否与给定的正则表达式匹配。

你需要:

public boolean contains(CharSequence s)

答案 1 :(得分:0)

            if (Pattern.compile(Pattern.quote(songToSearch), Pattern.CASE_INSENSITIVE).matcher(value).find()) {
            playMusic(songToSearch);
        }

会解决问题......我的帖子实际上是重复的