php中的方法真是不少,今天觉得自己实现一下substr_count。 由于今天比较忙,就没自己想实现方法,google了一下,发现了3种,其中的两个方法真是巧妙,我要是自己实现估计只能想到第一,也就是看起来最笨的那个。不过考虑一下性能,笨方法居然是最快的
package util;
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter;
/** * This class is used to test the performance of the method for getting how *** **/ public class CountSubString {
public static void main(String[] args) throws Exception{ CountSubString count = new CountSubString();
String source = ""; String sub = "tan"; StringBuffer sb = new StringBuffer();
for (int i = 0; i < 1000000; i++) { sb.append("tan"); sb.append("tory"); sb.append("eddie"); }
source = sb.toString();
//String newSource = count.replace("tan", "aa", source); String newSource = source.replaceAll("t", "aa"); //System.out.println(newSource);
//File outPut = new File("d:/eclipse3.4/outputfile.dat");
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/eclipse3.4/outputfile.dat")); bw.write(newSource); bw.flush(); bw.close(); long timeStart = System.currentTimeMillis(); int subStringCount = count.getCountByChar(source, sub); long timeEnd = System.currentTimeMillis();
System.out.println("Sub String occurs times is: " + subStringCount); System.out.println("The method getCountByChar consumes " + (timeEnd - timeStart));
timeStart = System.currentTimeMillis(); subStringCount = count.getCountByReplace(source, sub); timeEnd = System.currentTimeMillis(); System.out.println("Sub String occurs times is: " + subStringCount); System.out.println("The method getCountByReplace consumes " + (timeEnd - timeStart));
timeStart = System.currentTimeMillis(); subStringCount = count.getCountBySplit(source, sub); timeEnd = System.currentTimeMillis(); System.out.println("Sub String occurs times is: " + subStringCount); System.out.println("The method getCountBySplit consume " + (timeEnd - timeStart));
}
public int getCountByChar(String source, String sub) { int count = 0; int pos = source.indexOf(sub.charAt(0));
do { next: if (pos > -1) { // boolean isEqual = true; for (int j = 1; j < sub.length(); j++) { if (source.charAt(pos + j) != sub.charAt(j)) { // isEqual = false; break next; } }
// if (isEqual) count++;
}
pos = source.indexOf(sub.charAt(0), pos + 1);
} while (pos != -1);
return count; }
public int getCountBySplit(String source, String sub) { int count = 0;
String[] sourceArray = source.split(sub); count = sourceArray.length - 1;
return count; }
public int getCountByReplace(String source, String sub) { int count = 0;
count = (source.length() - source.replaceAll(sub, "").length()) / sub.length();
return count; }
}
java的实现有了,javascript就很好办了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <title>Sub String Count</title> <head> <script type="text/javascript"> //get the count of the sub string occurs in the source function getCountByChar(source, sub) { var count = 0; var pos = source.indexOf(sub.charAt(0));
do { //next: if (pos > -1) { var isEqual = true; for (var i = 1; i < sub.length; i++) { if (source.charAt(pos + i) != sub.charAt(i)) { isEqual = false; break; }
} if (isEqual) count++; } pos = source.indexOf(sub.charAt(0), pos + 1); } while(pos > -1)
return count; }
function getCountBySplit(source, sub) { var count = 0; count = source.split(sub).length - 1; return count; }
function getCountByReplace(source, sub) { var count = 0; var replacSource = source.replace(/tan/g, ""); count = (source.length - replacSource.length) / (sub.length) ; return count; }
function capSentence() { var str = document.getElementById("content").value; var result = document.getElementById("resultDiv"); result.innerHTML = str + escape("Programming PHP啊"); document.getElementById("resultChar").value = getCountByChar(str, "tan") + "getCountByChar"; document.getElementById("resultSplit").value = getCountBySplit(str, "tan") + "getCountBySplit"; document.getElementById("resultReplace").value = getCountByReplace(str, "tan") + "getCountByReplace" ; } </script>
<style type="text/css"> input.cap {text-transform: capitalize;} </style> </head>
<body>
<input id="content" class="cap" type="text" /> <input type="button" οnclick="capSentence();" value="Capitaliz" /> <div id="resultDiv"></div> <input id="resultChar" type="text" /> <input id="resultSplit" type="text" /> <input id="resultReplace" type="text" /> </body> </html>
调试过程中发现了几个问题, 第一, java的当字符串很长的时候replaceAll方法会抛outofmemory异常,字串匹配的越多,需要的内存就越大。给eclipse加上如下参数可以增加虚拟机的内存-Xms32m -Xmx256m 第二, 也就是昨天发现的javascript的replace方法,只有以/tan/g形式才能替换所有的字串,但目前只找到这种hardcode的方法,不知道怎么通过变量实现 |