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

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

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);
	}
	
}






JavaScript Js EcmaScript计算统计文档文本中字符串字段字符出现次数

方法1

txt.match(/str/g).length;

方法2

txt.split(str).length-1;

方法3

function strCountBy  (txt, str){
	let count = 0; 
	for(;;){let idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length); count++; }
	return count;
}

方法4

function strCountBy  (txt, str){
	let count = 0; 
	for(;;count++){let idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length);  }
	return count;
}

方法5

function strCountBy  (txt, str){
	let count = 0 , idx;
	while((idx=txt.indexOf(str,idx))>-1) { ++count; idx+=str.length; }
	return count;
}

方法6

function strCountBy  (txt, str){ let count=0 , pattern=RegExp(str,'g'); while(pattern.exec(txt))count++; return count;  }

方法7

function strCountBy  (txt, str){ let count=0 , pattern=RegExp(str,'g'); while(pattern.test(txt))count++; return count;  }

方法8

function strCountBy  (txt, str){ return txt.split(str).length-1; }

方法9

function strCountBy  (txt, str){ return txt.match(RegExp(str,'g')).length; }

split的速度是最快的

测试代码1

<!doctype html><body><script>



strCountBy_methods =[
	(txt, str)=>{ if( !txt || !str )return 0;
		let count = 0; 
		for(;;){let idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length); count++; }
		return count;
	}
	,
	(txt, str)=>{ if( !txt || !str )return 0;
		let count = 0; 
		for(;;count++){let idx=txt.indexOf(str); if(idx<0)break; txt=txt.substring(idx+str.length);  }
		return count;
	}
	,
	(txt, str)=>{ if( !txt || !str )return 0;
		let count = 0 , idx;
		while((idx=txt.indexOf(str,idx))>-1) { ++count; idx+=str.length; }
		return count;
	}
	,
	(txt, str)=>{  if( !txt || !str )return 0; let count=0 , pattern=RegExp(str,'g'); while(pattern.exec(txt))count++; return count;  }
	,
	(txt, str)=>{  if( !txt || !str )return 0; let count=0 , pattern=RegExp(str,'g'); while(pattern.test(txt))count++; return count;  }
	,
	(txt, str)=>{  if( !txt || !str )return 0; return txt.split(str).length-1; }
	,
	(txt, str)=>{  if( !txt || !str )return 0; let ar= txt.match(RegExp(str,'g')); if(ar)return ar.length; return 0;}
]




const txt = `
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
				0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

`


function test(str){
	const fl = document.createElement("fieldset"); fl.innerHTML="<legend>测试字段 = "+str+"</legend>"; document.body.append(fl);
	const b = document.createElement("section"); fl.append(b); b.style.cssText="display:grid; grid-template-columns:1fr 1fr 1fr 1fr 1fr 1fr; grid-auto-rows:100px; border:1px blue solid;  grid-gap:10px 30px;"; 
	strCountBy_methods.forEach(f=>{
		let count = f(txt, str);
		let lb = document.createElement("label");
		lb.innerHTML=(`<div style="font-size:25px; border:1px solid black; border-radius:5px; text-align:center; " >${count}</div>`);
		b.append(lb);
	});
}

test("Za"); test("zA"); test('789ABCD');  test('789ABCd'); test(); test(null); test(0); test(""); test("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy")



const 执行次数 = 1000000;
let t = document.createElement("section"); t.innerHTML=`下面测是执行<label style="font-size:188%; color:blue; ">${执行次数}</label>次的速度测试`;
t.style.cssText="font-size:16px; color:white; background:black;"
document.body.appendChild(t);




function testSpeed(str){
	const fl = document.createElement("fieldset"); fl.innerHTML="<legend>测试字段是 "+str+" , 测试其执行 "+执行次数+ "次的速度</legend>"; document.body.append(fl);
	const b = document.createElement("section"); fl.append(b); b.style.cssText="display:grid; grid-template-columns:1fr 1fr 1fr 1fr 1fr 1fr; grid-auto-rows:100px; border:1px blue solid;  grid-gap:10px 30px;"; 
	strCountBy_methods.forEach(f=>{
		let begin = Date.now();
		for(let i=0; i<执行次数 ; i++) f(txt, str);
		let cost = Date.now()-begin;
		let lb = document.createElement("label");
		lb.innerHTML=(`<div style="font-size:25px; border:1px solid black; border-radius:5px; text-align:center; " >耗时 ${cost} 毫秒</div>`);
		b.append(lb);
	});
}


testSpeed("Za"); testSpeed("zA"); testSpeed('789ABCD');  testSpeed('789ABCd'); testSpeed(); testSpeed(null); testSpeed(0); testSpeed(""); testSpeed("123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz			0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy");


</script></body>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值