栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java去掉字符串左侧(前缀)空格

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java去掉字符串左侧(前缀)空格

列出8种去除字符串前缀“空格”的方法如下 需要导入junit-4..11和hamcrest-core-1.3 jar
public class Test {

    
    @Test
    public void test1(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        sql=sql.trim();
//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test2(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        //while形式
        int i=0;
        while (sql.codePointAt(i)==32){
            i++;
        }
        sql=sql.substring(i,sql.length());


//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test3(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        while (sql.indexOf(" ")==0){
            sql=sql.substring(1, sql.length());
        }
//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test4(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        char[] chars = sql.toCharArray();

        //while形式
        int i=0;
        while (chars[i]==32){
            i++;
        }
        sql=sql.substring(i,sql.length());


//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test5(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        byte[] bytes = sql.getBytes();

        //while形式


        //for形式
        for (int i = 0; i < bytes.length; i++) {
            if(bytes[i]!=32){
                sql=sql.substring(i,sql.length());
                break;
            }
        }
//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test6(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        String[] strings = sql.split(" ");

        //while形式
        int i=0;
        while ("".equals(strings[i])){
            i++;
        }
        sql=sql.substring(i,sql.length());

        //for形式

//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test7(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        int i=0;
        while (sql.charAt(i)==32){
            i++;
        }
        sql=sql.substring(i,sql.length());
//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }

    
    @Test
    public void test8(){

        String sql="                                                                                                    select   * from zevin";
//        String sql="  select   * from zevin";

        long l = System.nanoTime();
//————————————————————————————————————————————————————
        sql=sql.replaceAll("^[ ]+", "");
//————————————————————————————————————————————————————
        System.out.println(System.nanoTime()-l);

        System.out.println(sql);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号