相关疑难解决方法(0)

Commons Lang StringUtils.replace性能vs.String.replace

当我比较Apache的性能StringUtils.replace()VS String.replace()我很惊讶地知道,前者是快约4倍.我使用Google的Caliper框架来衡量效果.这是我的考试

public class Performance extends SimpleBenchmark {
    String s = "111222111222";

    public int timeM1(int n) {
        int res = 0;
        for (int x = 0; x < n; x++) {
            res += s.replace("111", "333").length();
        }
        return res;
    }

    public int timeM2(int n) {
        int res = 0;
        for (int x = 0; x < n; x++) {
            res += StringUtils.replace(s, "111", "333", -1).length();
        }
        return res;
    }

    public static void main(String... args) {
        Runner.main(Performance.class, args);
    } …
Run Code Online (Sandbox Code Playgroud)

java

39
推荐指数
4
解决办法
4万
查看次数

使用java从文本中删除url

如何删除文本示例中的URL

String str="Fear psychosis after #AssamRiots - http://www.google.com/LdEbWTgD http://www.yahoo.com/mksVZKBz";
Run Code Online (Sandbox Code Playgroud)

使用正则表达式?

我想删除文本中的所有网址.但它没有用,我的代码是:

String pattern = "(http(.*?)\\s)";
Pattern pt = Pattern.compile(pattern);
Matcher namemacher = pt.matcher(input);
if (namemacher.find()) {
  str=input.replace(namemacher.group(0), "");
}
Run Code Online (Sandbox Code Playgroud)

java regex

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×2

regex ×1