如何计算字符串的子串已出现在另一个大字符串中?

时间:2014-02-27 14:08:24

标签: java string substring

我有一个像“ali li vali bali”这样的大字符串,我想知道所有单词(我的意思是像li这样的单词)被重复多次(ali,li,vali和bali只被视为子字符串)除了自己的存在。

例如:li是一个大字符串中的子字符串,除了它自己的存在之外它重复三次。

li in ali,li in vali and li in bali。数量是:3。

这是我的代码和我收到的错误:

import java.util.*;
public class main{
    public static void main(String args[]){
        String str="";
        Scanner scan= new Scanner(System.in);
        while(scan.hasNext())
        {
            str+= scan.nextLine();
        } 
        int n= str.trim().split("\\s+").length;
        String[] wordsArray=str.split(" ");
        substring sub=new substring();
        for(int i=0;i<wordsArray.length;i++){
            int count=sub.sub(wordsArray[i],str);
            csub+=count;
        }
        System.out.println("number of substring: "+csub);

和子字符串执行:

public class substring{
    public int sub(String str,String str2){
        String[] wordsArray=str2.split(" ");
        int len=str.length();
        int b=0;
        String[] str1=new String[len*2+2];
        int k=0;
        for (int from = 0; from < str.length(); from++) {
            for (int to = from + 1; to <= str.length(); to++) {
                str1[k]=str.substring(from, to);
                k++;
            }
        }
        int index = 0;
        int count = 0;
        for(int i=0;i<str1.length;i++){
            if(str1[i].length()>2){
                while ((index = str2.indexOf(str1[i], index)) != -1) {
                    index += str1[i].length();
                    count++;
                    b++;
                }
            }
        }
        return b;
    }
}

有什么问题?

**I got this error:**                 
Expection in thread "main" java lang.NullPointerExpection                    
at substring.sub<substring.java:20>                                   
at main.main<main.java:52>

2 个答案:

答案 0 :(得分:1)

您只需扫描字符串而不拆分它。

示例:

public static int countSubstring(String str, String substring) {
    // start at the beggining of the string
    int pos = 0; 
    int count = 0;

    // search starting at pos and store the index (if found) on pos
    while ((pos = str.indexOf(substring, pos)) != -1) {
        count++;

        // pos is at the index of last found substring
        // advance it by the length of the substring
        pos = pos + substring.length();
    }
    return count;
}

public static void main(String[] args) {
    System.out.println(countSubstring("li, vali and bali", "li"));
}

输出

3

答案 1 :(得分:0)

你可以这样做,

public static int sub(String str, String str2) {
    if (str == null || str2 == null) {
        return 0;
    }
    int count = 0;
    int p = str2.indexOf(str);
    while (p >= 0) {
        count++;
        p = str2.indexOf(str, p + str.length());
    }
    return count;
}

public static void main(String[] args) throws Exception {
    String toSearch = "Hi Dee Hi";
    System.out.println(sub("Hi", toSearch));
    System.out.println(sub("Dee", toSearch));
    System.out.println(sub("Ho", toSearch));
}

输出

2
1
0