Java计算统计文档文本中字符串字段字符出现次数

324 阅读1分钟

Java计算统计文档文本中字符串字段字符出现次数

方法1

	public static int strCountBy (String txt, String str) {
		if( txt==null || str==null )return 0;
		return txt.split(str).length-1;
	}

方法2

	public static int strCountBy2 (String txt, String str) {
		if( txt==null || str==null )return 0;
		int count=0 , idx=0; for(;;){ idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length()); ++count; }
		return count;
	}

方法3

	public static int strCountBy3 (String txt, String str) {
		if( txt==null || str==null )return 0;
		java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(str).matcher(txt);
		int count=0;      while(matcher.find())count++;
		return count;
	}

方法4

	public static int strCountBy4 (String txt, String str) {
		if( txt==null || str==null )return 0;
		int count=0 , idx=0; while((idx=txt.indexOf(str,idx))>-1) {++count; idx+=str.length(); }
		return count;
	}

方法5, 要用到第三方jar包, Apache的commons-lang的StringUtils的countMatches方法

	public static int strCountBy5 (String txt, String str) {
		return StringUtils.countMatches(txt, str);
	}

测试1

package p2306;

import org.apache.commons.lang.StringUtils;

public class T2306022205 {
	
	public static int strCountBy (String txt, String str) {
		if( txt==null || str==null )return 0;
		return txt.split(str).length-1;
	}
	
	public static int strCountBy2 (String txt, String str) {
		if( txt==null || str==null )return 0;
		int count=0 , idx=0; for(;;){ idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length()); ++count; }
		return count;
	}
	
	public static int strCountBy3 (String txt, String str) {
		if( txt==null || str==null )return 0;
		java.util.regex.Matcher matcher = java.util.regex.Pattern.compile(str).matcher(txt);
		int count=0;      while(matcher.find())count++;
		return count;
	}
	
	public static int strCountBy4 (String txt, String str) {
		if( txt==null || str==null )return 0;
		int count=0 , idx=0; while((idx=txt.indexOf(str,idx))>-1) {++count; idx+=str.length(); }
		return count;
	}
	
	public static int strCountBy5 (String txt, String str) {
		return StringUtils.countMatches(txt, str);
	}
	
	
	
	
	
	
	public static void main(String...arguments)throws Exception{
		String text = """
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				""";
		
		int count =0;
		String str = "abcdefg";
		
		count = strCountBy  (text , str); System.out.println(count);
		count = strCountBy2 (text , str); System.out.println(count);
		count = strCountBy3 (text , str); System.out.println(count);
		count = strCountBy4 (text , str); System.out.println(count);
		count = strCountBy5 (text , str); System.out.println(count);
	}
	
}