Java 中字符串的处理

字符串比较

函数:
compareTo(string)
compareTpIgnoreCase(String)
compareTo(object string)
返回字符串中第一个字母 ASCII 的差值。
实例:

package demo3;

public class Demo1 {
    public static void main(String[] args) {
        String str = "Hello World!";
        String anotherString = "hello World!";
        Object objStr = str;

        System.out.println( str.compareTo(anotherString) );
        System.out.println( str.compareToIgnoreCase(anotherString) );//忽略大小写
        System.out.println( str.compareTo(objStr.toString()) );
    }
}

查找字符串最后一次出现的位置

函数:strOrig.lastIndexOf(Stringname)

实例:

package demo3;

public class Demo2 {
    public static void main(String[] args) {
        String strOrig = "Hello world , Hello Runoob";
        int lastIndex  = strOrig.lastIndexOf("Runoob");
        if (lastIndex == -1) {
            System.out.println("没有找到字符串 Runoob");
        } else {
            System.out.println("Runoob 字符串最后出现的位置:" + lastIndex);
        }
    }
}

删除字符串中的一个字符

函数:substring()

实例:

package demo3;

public class Demo3 {

    public static void main(String[] args) {
        String str = "this is Java";
        System.out.println(removeCharAt(str,3));
    }
    public static String removeCharAt(String str,int pos) {
        return str.substring(0, pos) + str.substring(pos + 1);
    }

}

字符串替换

函数:replace(a,b)

a: 要被替代掉的原字符[串]
b: 替代原字符[串]的新字符[串]

实例:

package demo3;

public class Demo4 {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println( str.replace("H", "W") );
        System.out.println( str.replaceFirst( "He", "Wa") );
        System.out.println( str.replaceAll("He", "Ha") );
    }
}

字符串反转

函数:reverse()

实例:

package demo3;

public class Demo5 {
    public static void main(String[] args) {
        String str = "Hello";
        String reverse = new StringBuffer(str).reverse().toString();
        System.out.println("字符串反转前:" + str);
        System.out.println("字符串反转后:" + reverse);
    }
}

字符串搜索

函数:indexOf()

实例:

package demo3;

public class Demo6 {
    public static void main(String[] args) {
        String str = "Google Runoob Taobao";
        int intIndex = str.indexOf("Runoob");
        if (intIndex == -1) {
            System.out.println("没有找到字符串 Runoob");
        } else {
            System.out.println("Runoob 字符串位置:" + intIndex);
        }
    }
}

字符串分割

函数:split(string)

实例:

package demo3;

public class Demo7 {
    public static void main(String[] args) {
        String str = "www-runoob-com";
        String[] temp;
        String delimeter = "-";// 指定分割字符
        temp = str.split(delimeter);// 分割祖字符串
        // 普通 for 循环
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
            System.out.println("");
        }
        System.out.println("------java for each 循环输出的方法-------");
        String str1 = "www.runoob.com";
        String[] temp1;
        String delimeter1 = "\\.";// 指定分割字符, . 号需要转义
        temp1 = str1.split(delimeter1); // 分割字符串
        for(String x : temp1) {
            System.out.println(x);
            System.out.println("");
        }
    }
}

字符串小写转大写

函数:String toUpperCase()

实例:

package demo3;

public class Demo8 {
    public static void main(String[] args) {
        String str = "string runoob";
        String strUpper = str.toUpperCase();
        System.out.println("原是字符串:" + str);
        System.out.println("转换为大写:" + strUpper);
    }
}

测试两个字符串区域是否想等

函数:regionMatches()

实例:

package demo3;

public class Demo9 {
    public static void main(String[] args) {
        String first_str = "Welcome to Microsoft";
        String second_str = "I work with microsoft";
        boolean match1 = first_str.regionMatches(11, second_str , 12, 9);
        boolean match2 = first_str.regionMatches(true, 11, second_str, 12,9);// 第一个参数  true 表示忽略大小写
        System.out.println("区分大小写返回值:" + match1);
        System.out.println("不区分大小写返回值:" + match2);
    }
}

字符串性能比较测试

实例:

package demo3;

public class Demo10 {
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            String s1 = "hello";
            String s2 = "hello";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("通过 String 关键词创建字符串:" + (endTime - startTime) + "毫秒");

        long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            String s3 = new String("hello");
            String s4 = new String("hello");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("通过 String 对象创建字符串:" + (endTime1 - startTime1) + "毫秒");
    }
}

结果:

通过 String 关键词创建字符串:1毫秒
通过 String 对象创建字符串:4毫秒

证明用 String 关键词创建字符串效率更好。

字符串优化

函数:String.intern()

实例:

package demo3;

public class Demo11 {
    public static void main(String[] args) {
        String variables[] = new String[50000];
        for (int i = 0; i < 50000; i++) {
            variables[i] = "s" + i;
        }

        long startTime0 = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            variables[i] = "hello";
        }
        long endTime0 = System.currentTimeMillis();
        System.out.println("直接使用字符串:" + (endTime0 - startTime0) + "毫秒");

        long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            variables[i] = new String("hello");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("使用 new 关键字:" + (endTime1 - startTime1) + "毫秒");

        long startTime2 = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            variables[i] = new String("hello");
            variables[i] = variables[i].intern();
        }
        long endTime2 = System.currentTimeMillis();
        System.out.println("使用字符串对象的  intern() 方法:" + (endTime2 - startTime2) + "毫秒");
    }
}

结构:

直接使用字符串:1毫秒
使用 new 关键字:2毫秒
使用字符串对象的 intern() 方法:6毫秒

字符串格式化

函数:format()

实例:

package demo3;

import java.util.Locale;

public class Demo12 {
    public static void main(String[] args) {
        double e = Math.E;
        System.out.format("%f%n",e);
        System.out.format(Locale.CHINA,"%-10.4f%n%n", e );// 指定本地为 CHINA
    }
}

结果:

2.718282
2.7183

连接字符串

函数:+ 或者 StringBuffer.append()

实例:

package demo3;

public class Demo13 {

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            String result = "This is" + "testing the" + "difference" + "between" + "String" + "and" + "StringBuffer";
        }
        long endTime = System.currentTimeMillis();
        System.out.println("字符串连接-使用 + 操作符:" + (endTime - startTime) +"毫秒");

        long startTime1 =System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            StringBuffer result = new StringBuffer();
            result.append("This is");
            result.append("testing the");
            result.append("difference");
            result.append("between");
            result.append("String");
            result.append("and");
            result.append("StringBuffer");
        }
        long endTime1 = System.currentTimeMillis();
        System.out.println("字符串连接-使用 StringBuffer :" + (endTime1 - startTime1) + "毫秒");
    }
}

结果:

字符串连接-使用 + 操作符:0毫秒
字符串连接-使用 StringBuffer :3毫秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值