JAVA练手--String

package tet;


public class kk {
    
    public static void main(String[] args) {
        //1.
        {
            String Stra = "123abc";
            //得到指定索引处的char值
            char ca = Stra.charAt(3);
            System.out.println("1:  "+ca);    
        }
        //2. compareTo比较两个对象的差值,compareToIgnoreCase忽略大小写,返回值都为差值
        {
            String Stra = "abc";
            String Strb = "ABC";
            String Strc = "aBc";
            System.out.println("2:  "+Stra.compareTo(Strb));
            System.out.println("2:  "+Stra.compareToIgnoreCase(Strc));
        }
        //3. 指定字符串在最后一次出现的位置
        {
            String Stra = "hello world  haha hello";
            int i = Stra.lastIndexOf("haha");
            System.out.println("3:  "+i);
            System.out.println("3:  "+Stra.lastIndexOf("hello"));
        }
        //4. 删除字符串中的一个字符
        {    //减去hello中的o
            String Stra = "hello world  haha";
            String Strb = Stra.substring(0, 4)+Stra.substring(5);
            System.out.println("4:  "+Strb);
        }
        //5. 替换字符串,注意:是返回替换后的,本身不会替换
        {
            String Stra = "hello world  haha";
            Stra.replace("hehe", "haha");
            System.out.println("5:  "+Stra);
            
            String Strb = Stra.replace("haha", "hehe");
            System.out.println("5:  "+Strb);
        }
        //6. 查找字符串
        {
            String Stra = "hello world  haha";
            System.out.println("6:  "+Stra.indexOf("world"));
            System.out.println("6:  "+Stra.indexOf("worle"));
        }
        //7. 字符串分割
        {
            String Stra = "www-baidu-com";
            String[] suba;
            suba = Stra.split("-");
            for(String x : suba){
                System.out.println("7:  "+x);
            }
            
            String Strb = "xxx hhh  ccc";  //注意:ccc前面有两个空格,第一个会检测到,第二个不是的了
            String[] subb;
            subb = Strb.split(" ");
            for(String x : subb){
                System.out.println("7:  "+x);
            }
        }
        //8. 去掉前后空格
        {
            String Stra = "   hello   ";
            System.out.println("8:  "+Stra+Stra.length());
            String Strb = Stra.trim();
            System.out.println("8:  "+Strb+Strb.length());
        }
        //9. 大小写转换
        {
            String Stra = "hello";
            String Strb = "HELLO";
            String Strc = "HellO";
            System.out.println("9:  "+Stra.toUpperCase());  //小写转大写
            System.out.println("9:  "+Strb.toLowerCase());  //大写转小写
            System.out.println("9:  "+Strc.toUpperCase());  //有大写有小写的字符串也能转换
        }
        //10. 格式化字符串
        {
            int i=5;double d = 6.6;
            String Stra = String.format("haha=%d,hehe=%f",i,d);
            System.out.println("10: "+Stra);  
        }
        //11. 连接字符串
        {
            String Stra = "hello";
            String Strb = Stra.concat("  hehe");
            System.out.println("11: "+Stra);
            System.out.println("11: "+Strb);  
        }
        //12. 字符串性能测试
        {
            double current = System.currentTimeMillis();
            for(int i=0;i<500000;i++){
                String a = "hello";
            }
            double end = System.currentTimeMillis();
            System.out.println("12: 耗时="+(end-current)+"毫秒");  
            
            double current1 = System.currentTimeMillis();
            for(int i=0;i<500000;i++){
                String a = new String("haha");
            }
            double end1 = System.currentTimeMillis();
            System.out.println("12: 耗时="+(end1-current1)+"毫秒"); 
        }
    }

}

运行结果:

1:  a
2:  32
2:  0
3:  13
3:  18
4:  hell world  haha
5:  hello world  haha
5:  hello world  hehe
6:  6
6:  -1
7:  www
7:  baidu
7:  com
7:  xxx
7:  hhh
7:  
7:  ccc
8:     hello   11
8:  hello5
9:  HELLO
9:  hello
9:  HELLO
10: haha=5,hehe=6.600000
11: hello
11: hello  hehe
12: 耗时=7.0毫秒
12: 耗时=25.0毫秒
原文地址:https://www.cnblogs.com/maogefff/p/8082160.html