Java String (string) class creation and common methods

public class str {
    
    
    public static void main(String[] args) {
    
    
        // 创建字符串的常用方式
        // 创建位置是在公共池,即s1 == s2 == s3
        String s1 = "Hello"; // String 直接创建
        String s2 = "Hello"; // String 直接创建
        String s3 = s1; // 相同引用

        // 创建位置是在堆,即s4 != s5
        String s4 = new String("Hello"); // String 对象创建
        String s5 = new String("Hello"); // String 对象创建
        // 使用字符数组创建
        char[] charArray = {
    
     'H', 'e', 'l', 'l', 'o' };
        String s6 = new String(charArray);

        // 常用方法
        // 1.返回字符串长度
        System.out.println(s1.length());
        // 2.链接两个字符串,两种方式
        System.out.println(s1.concat(s2));
        System.out.println(s1 + s2);
        // 3.返回指定索引处的 char 值。
        System.out.println(s1.charAt(2));
        // 4.把这个字符串和另一个对象从左往右比较。
        // 返回的是第一个不同字符的ASCII码的差值(比较-被比较),这里返回-4
        System.out.println("123".compareTo("165"));
        // 5.把这个字符串和另一个对象从左往右比较,忽略大小写。
        // 返回的是第一个不同字符的ASCII码的差值(比较-被比较),这里返回-4
        System.out.println("1a23".compareToIgnoreCase("1A65"));
        // 6.当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真.
        // 相对比较,使用equals更多
        StringBuffer sb = new StringBuffer("1a23");
        System.out.println("1a23".contentEquals("1a23"));
        // 7.返回指定*数组*中表示该字符序列的 String。 即截取操作
        char[] Str1 = {
    
     'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!' };
        String Str2 = "";
        Str2 = Str2.copyValueOf(Str1); // 截取全部
        System.out.println("返回结果:" + Str2);
        Str2 = Str2.copyValueOf(Str1, 2, 7);
        System.out.println("返回结果:" + Str2);
        // 8.测试此字符串是否以指定的后缀结束。
        System.out.println(s4.endsWith("llo"));
        // 9.将此 String 与另一个 String 比较,不考虑大小写。
        System.out.println("hello".equalsIgnoreCase("HELLO"));
        // 10.返回指定字符在此字符串中第一次出现处的索引。
        System.out.println("Hello".indexOf('l'));
        // 11.返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
        System.out.println("hello world".indexOf('l', 4));
        // 12.返回指定子字符串在此字符串中第一次出现处的索引。
        System.out.println("hello world".indexOf("wor"));
        // 13.返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
        System.out.println("hello world".indexOf("wor", 3));
        // 14. 返回指定字符在此字符串中最后一次出现处的索引。
        System.out.println("hello world".lastIndexOf('l'));
        // 15.返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
        System.out.println("hello world".lastIndexOf('l', 4));
        // 16.返回指定子字符串在此字符串中最右边出现处的索引。
        System.out.println("hello worlld".lastIndexOf("ll"));
        // 17.返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
        System.out.println("hello worlld".lastIndexOf("ll", 3));
        // 18.告知此字符串是否匹配给定的正则表达式。也可以查是否包含要查找的子串
        System.out.println("hello world".matches("(.*)wor(.*)"));
        // 19.测试两个字符串区域是否相等。
        /**
         * public boolean regionMatches(boolean ignoreCase, int toffset, String other,
         * int ooffset, int len) 参数 ignoreCase -- 如果为 true,则比较字符时忽略大小写。默认为false toffset
         * -- 此字符串中子区域的起始偏移量。 other -- 字符串参数。 ooffset -- 字符串参数中子区域的起始偏移量。 len --
         * 要比较的字符数。
         */
        System.out.println("hellohellohello".regionMatches(true, 5, "hello", 0, 5));
        // 20.返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
        System.out.println("*****".replace("*", "+"));
        // 21.replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。
        /**
         * public String replaceAll(String regex, String replacement) regex --
         * 匹配此字符串的正则表达式。 newChar -- 用来替换每个匹配项的字符串。 成功则返回替换的字符串,失败则返回原始字符串。
         */
        System.out.println("hello world".replaceAll("(.*)world(.*)", "nice!!"));
        // 22.replaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。
        /**
         * public String replaceFirst(String regex, String replacement) regex --
         * 匹配此字符串的正则表达式。 replacement -- 用来替换第一个匹配项的字符串。 成功则返回替换的字符串,失败则返回原始字符串。
         */
        System.out.println("hello world".replaceFirst("(.*)world(.*)", "nice!!"));
        // 23.split() 方法根据匹配给定的正则表达式来拆分字符串。
        // public String[] split(String regex, int limit)
        /**
         * regex -- 正则表达式分隔符。 limit -- 分割的份数。 返回值:字符串数组。
         */
        System.out.println("hello - hello - world".split("-", 2));
        // 24.测试此字符串是否以指定的前缀开始。
        System.out.println("dehello".startsWith("de"));
        // 25.测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
        System.out.println("0123dehello".startsWith("de", 3));
        // 26.subSequence() 方法返回一个新的字符序列,它是此序列的一个子序列。
        // public CharSequence subSequence(int beginIndex, int endIndex)
        // 相同的方法还有:
        // String substring(int beginIndex)
        // String substring(int beginIndex, int endIndex)
        /**
         * 截取操作 参数: beginIndex -- 起始索引(包括)。 endIndex -- 结束索引(不包括)。 返回值:返回截取的部分
         */
        System.out.println("0123hello9".subSequence(4, 9));
        // 27.将此字符串转换为一个新的字符数组。
        System.out.println("hello".toCharArray());
        // 28.使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
        // String toLowerCase()
        // 29. 返回此对象本身(它已经是一个字符串!)。
        // String toString()
        // 30.使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
        // String toUpperCase()
        // 31.返回字符串的*副本*,忽略前导空白和尾部空白。
        // String trim()
        // 32.返回给定data type类型x参数的字符串表示形式。
        /**
         * valueOf(boolean b): 返回 boolean 参数的字符串表示形式。. valueOf(char c): 返回 char
         * 参数的字符串表示形式。 valueOf(char[] data): 返回 char 数组参数的字符串表示形式。 valueOf(char[] data,
         * int offset, int count): 返回 char 数组参数的特定子数组的字符串表示形式。 valueOf(double d): 返回
         * double 参数的字符串表示形式。 valueOf(float f): 返回 float 参数的字符串表示形式。 valueOf(int i): 返回
         * int 参数的字符串表示形式。 valueOf(long l): 返回 long 参数的字符串表示形式。 valueOf(Object obj): 返回
         * Object 参数的字符串表示形式。
         */
        // 33.contains() 方法用于判断字符串中是否包含指定的字符或字符串。
        // public boolean contains(CharSequence chars)
        System.out.println("hello world".contains("o w"));
        // 34.判断字符串是否为空。
        // isEmpty()
    }
}

Guess you like

Origin blog.csdn.net/wjl__ai__/article/details/112299267