java字符串中搜索子串

 
package com.duapp.itfanr;

import java.util.*;

public class CharDemo {

	public static void main(String args[]) {

		String str = "abcdabegababefgab";
		// char charVect[]=str.toCharArray();
		String subStr = "ab";
		int lenSub = subStr.length();
		int count = 0;
		for (int i = 0; i < str.length() - lenSub+1 ; i++) {
			StringBuffer sb = new StringBuffer(str.subSequence(i, i + lenSub));
			if (compare(sb.toString(), subStr))
				count++;
		}
		System.out.println(count) ;//此处结果为5
	}

	static boolean compare(String str, String subStr) {
		// boolean flag ;

		if (str.equals(subStr))
			return true;
		else
			return false;
	}

}
 
package com.handy.ds;  

import JAVA.util.Stack;  
import java.util.Arrays;  

;  

public class TestString {  

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        TestString ts = new TestString();  
        System.out.println(ts.compareTo1("abababab", "bab"));  
        System.out.println(ts.compareTo2("abababab", "bab"));  
        System.out.println(ts.compareTo3("abababab", "bab"));  

    }  

    // 想法:如str1=ababac,str2=ab  
    // 往后取str1的所有str2长度(2)的组合,ab,ba,ab,ba,ac  
    // 将这些组合分别与str2比较,相等则count+1  

    public String compareTo1(String str1, String str2) {  

        String ss = null;  
        int count = 0;  
        for (int i = 0; i <= str1.length() - str2.length(); i++) {  
            ss = str1.substring(i, i + str2.length());  
            if (ss.equals(str2))  
                count++;  

        }  

        return String.valueOf(count);  
    }  

    // 想法:如str1=ababac,str2=ab  
    // 往后取str1的所有2长度(2)的组合,ab,ba,ab,ba,ac  
    // 将这些组合分别与str2比较,相等则count+1  
    // 跟第一种方法类似,不过用栈来存罢了  
    public String compareTo2(String str1, String str2) {  
        Stack s = new Stack();  

        int count = 0;  
        for (int i = 0; i <= str1.length() - str2.length(); i++) {  
            s.add(str1.substring(i, i + str2.length()));  

        }  
        int i = 0;  
        while (s.size() != 0) {  
            if (s.pop().toString().equals(str2))  
                i++;  

        }  
        return String.valueOf(i);  

    }  

    // 想法:找到str1中与str2第一个字母相同的位置,然后往后取与str2相同长度的子串,和str2比较,相等则count+1  
    // 如str1=abcab,str2=ab, str1中a与str2的a相同,往后取ab,与str2相等,count+1  
    // 然后取bc,ca,ab 又有一个ab 故count=2  
    public String compareTo3(String str1, String str2) {  
        int count = 0;  
        int i = 0;  
        String temp = null;  
        char c;  
        boolean isCount = false;  

        if (str1.length() < str2.length())  
            return "0";  
        while (str2.length() + i <= str1.length()) {  

            c = str1.charAt(i);  

            if (c == str2.charAt(0)) {  
                temp = str1.substring(i, i + str2.length());  
                isCount = true;  
            } else  
                isCount = false;  
            if (isCount && temp.equals(str2))  
                count++;  
            i++;  

        }  

        return String.valueOf(count);  
    }  
}
参考: [1].http://www.bcwhy.com/thread-9864-1-1.html

你可能感兴趣的:(java,String,字符串,搜索)