java String

字符串是不变对象,内容一旦创建不可改变,若改变一定会创建新对象。

频繁修改字符串带来的性能开销。

        String s1 = "123hello";
        String s2 = "123hello";        
        System.out.println(s1==s2);//true
        
        String s3 = "123hello";
        System.out.println(s1==s3);//true
        
        s1 = s1+"!";//修改内容会创建新对象
        System.out.println("s1:"+s1); //s1:123hello!
        
        System.out.println("s2:"+s2); //s2:123hello
        System.out.println(s2==s3); //true
        System.out.println(s1==s2);//false
        /*
         * new创建的字符串对象并不会重用
         */
        String s4 = new String("123hello");
        System.out.println(s4); //123hello
        System.out.println(s2==s4); //false
        System.out.println(s2.equals(s4)); //true

StringBuilder:

/**
 * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作
 * 的性能.因此String不适合频繁修改内容.
 * 若有频繁修改操作,使用StringBuilder来完成,它是专门设计出来为了修改字符串内容的,其提供了对字符串内容编辑操作所对应的:增,删,改,插
 * @author Administrator
 *
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
        String line = "好好学习java";
        //默认表示空字符串
//        StringBuilder builder = new StringBuilder();
        
        //基于给定字符串修改
        StringBuilder builder 
            = new StringBuilder(line);
        /*
         * 好好学习java,为了找个好工作!
         */
        builder.append(",为了找个好工作!");
        System.out.println(builder); //好好学习java,为了找个好工作!
        
        /*
         * 好好学习java,为了找个好工作!
         * 好好学习java,就是为了改变世界!
         */
        builder.replace(9, 16, "就是为了改变世界");
        System.out.println(builder); //好好学习java,就是为了改变世界!
        
        /*
         * 好好学习java,就是为了改变世界!
         * ,就是为了改变世界!
         */
        builder.delete(0, 8);
        System.out.println(builder); //,就是为了改变世界!
        
        /*
         * ,就是为了改变世界!
         * 活着,就是为了改变世界!
         */
        builder.insert(0, "活着");
        System.out.println(builder); //活着,就是为了改变世界!
    }
}

String substring(int start,int end)
 截取当前字符串中指定范围内的字符串:

/**
 * String substring(int start,int end)
 * 截取当前字符串中指定范围内的字符串.
 * 
 * java api有一个特点:通常用两个数字表示范围时,都是
 * "含头不含尾"的.
 * @author Administrator
 *
 */
public class SubstringDemo {
    public static void main(String[] args) {
        //             01234567890
        String line = "www.tedu.cn";
        
        String sub = line.substring(4, 8);
        System.out.println(sub); //tedu
        //重载方法可以从指定位置截取到字符串末尾
        sub = line.substring(4);
        System.out.println(sub); //tedu.cn
    }
}
/**
 * char charAt(int index)
 * 返回指定位置对应的字符
 * @author Administrator
 *
 */
public class CharAtDemo {
    public static void main(String[] args) {
        //            0123456789012345
        String str = "thinking in java";
        
        char c = str.charAt(1);
        System.out.println(c);
        
        /*
         * 检测回文
         */
        //             0 1 2 3  5 6 7 8
        String line = "上海自来水自来海上";
        for(int i=0;i<line.length()/2;i++) {
            char c1 = line.charAt(i);
            char c2 = line.charAt(line.length()-1-i);
            if(c1!=c2) {
                System.out.print("");
                break;
            }
        }
        System.out.println("是回文!");
        
    }
}
/**
 * 字符串支持正则表达式方法一:
 * boolean matches(String regex)
 * 使用给定的正则表达式判断当前字符串是否满足格式要求,满足
 * 则返回true.
 * 注意,该方法是做完全匹配验证的,无论是否添加正则表达式中
 * 的边界匹配符"^...$"都是做全匹配验证.
 * @author Administrator
 *
 */
public class MatchesDemo {
    public static void main(String[] args) {
        String email = "fancq@tedu.cn";
        /*
         * [a-zA-Z0-9_]+@[a-zA-Z0-9]+(.[a-zA-Z]+)+
         */
        String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\.[a-zA-Z]+)+";
        boolean check = email.matches(regex);
        if(check) {
            System.out.println("是邮箱");
        }else {
            System.out.println("不是邮箱");
        }
        
    }
}
/**
 * 字符串支持正则表达式方法二:
 * String[] split(String regex)
 * 将当前字符串按照满足正则表达式要求的部分进行拆分,并将拆分出的内容返回
 * 
 * @author Administrator
 *
 */
public class SplitDemo {
    public static void main(String[] args) {
        String str = "abc123def456ghi";
        /*
         * 按照数字部分将字符串拆分
         */
//        String[] data = str.split("[0-9]+");
        /*
         * 拆分时,若两侧没有内容时,会拆分出空字符串
         * 如果是在字符串末尾连续拆分出空字符串,那么都会
         * 被忽略
         * 用下面的方式拆分时:
         * abc123def456ghi
         * 与
         * abc123def456ghikahdshfakjshfashjkkdsjkfa
         * 拆分出的数组长度是一样的.
         * 
         */
        String[] data = str.split("[a-z]");
        System.out.println(data.length); //7
        System.out.println(Arrays.toString(data)); //[, , , 123, , , 456]
        
        
        String imageName = "123.jpg";
        String[] arr = imageName.split("\."); 
        System.out.println(arr.length); //2
        System.out.println(Arrays.toString(arr)); //[123, jpg]
        
        imageName = System.currentTimeMillis()+"."+arr[arr.length-1];
        System.out.println(imageName); //1577198202052.jpg
        
    }
    
    
}
原文地址:https://www.cnblogs.com/jyy599/p/12093977.html