Java基础知识总结(6)

String类中常用的类方法:

方法名称描述
format(String format, Object... args)使用指定的格式字符串和参数返回一个格式化字符串。 format - 格式字符串 args - 格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数,则忽略这些额外的参数。参数的数目是可变的,可以为 0。参数的最大数目受 Java Virtual Machine Specification 所定义的 Java 数组最大维度的限制。有关 null 参数的行为依赖于转换。
valueOf(Object obj)返回 Object 参数的字符串表示形式。
valueOf(char[] data)返回 char 数组参数的字符串表示形式。字符数组的内容已被复制,后续修改不会影响新创建的字符串。
valueOf(char[] data, int offset, int count)返回 char 数组参数的特定子数组的字符串表示形式。 offset 参数是子数组的第一个字符的索引。count 参数指定子数组的长度。字符数组的内容已被复制,后续修改不会影响新创建的字符 data - 字符数组。 offset - String 值的初始偏移量。 count - String 值的长度。
copyValueOf(char[] data, int offset, int count)返回 char 数组参数的特定子数组的字符串表示形式。 data - 字符数组。 offset - String 值的初始偏移量。 count - String 值的长度。
copyValueOf(char[] data)返回指定数组中表示该字符序列的 String。 data - 字符数组。
valueOf(boolean b)返回 boolean 参数的字符串表示形式。
valueOf(char c)返回 char 参数的字符串表示形式。
valueOf(int i)返回 int 参数的字符串表示形式。
valueOf(long l)返回 long 参数的字符串表示形式。
valueOf(float f)返回 float 参数的字符串表示形式。
valueOf(double d)返回 double 参数的字符串表示形式。
join(String s,str1,str2....)以s为连接符,连接字符串
public class Customer {
​
     @Override
     public String toString() {
        return "我是重写的toString方法!";
     }
​
}
​
public class StringTest4{   
    public static void main(String[] args){
         /*
         * 静态方法
         * String中只有一个方法是静态的,不需要new对象
         * 将"非字符串转换为字符串"
         * */
         double d1 = 0.023;
         System.out.println(String.format("%.2f", d1));
         
         char [] chars = {'x','y','z'};
         System.out.println(String.copyValueOf(chars));//xyz
         System.out.println(String.copyValueOf(chars,1,2));//yz
        
        System.out.println("类方法");
        //String s = String.valueOf(true);//把boolean类型转换为字符串
        //String s = String.valueOf(3.14);//把浮点类型转换为字符串
        //System.out.println(s);
        
        String  s= String.valueOf(new Customer());
        //System.out.println(s);//没有重写toString方法之前是对象的内存地址test.Customer@7de26db8
        System.out.println(s);
        
        //
        System.out.println(String.join(".", "www","baidu.com"));//www.baidu.com
    }
}

String类中常用的实例方法:

方法名称描述
equals(String string)判断字符串是否相等
equalslgnoreCase(String string)忽略大小写判断是否相等
length()获取字符串的长度
charAt(int index)获取某个索引处的字符(char)
indexOf(String string)返回指定子字符串在此字符串中第一次出现处的索引。(返回int类型的索引,找不到返回-1)
indexOf(String string,int startIndex)返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引startIdex开始。
lastindexOf(String string)返回指定子字符串在此字符串中最后一次出现处的索引。
lastindexOf(String str, int endsIndex)返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引endsIndex开始反向搜索。
startsWith(String string)判断是否以"string"开始
endsWith(String string)判断是否以"string"结束
comparTo(String string)比较字符串的大小
toLowerCase()将字符串转化为小写
toUpperCase()将字符串转换为大写
subString(int index)从index位置到截取到字符串的末尾
subString(int startindex,int endsindex)从开始索引位置开始到结束索引位置结束,前闭后开区间
trim()去除字符串前后的空格
replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
spilt(String string)根据给定正则表达式的匹配拆分此字符串。
split(String string,int limit)分割,保留末尾的空字符
concat(String string)将指定字符串连接到此字符串的结尾。
contains(String string)判断是否含string
toCharArray将字符串转换为字符数组
intern()返回字符串对象的规范化表示形式。</br>一个初始为空的字符串池,它由类 String 私有地维护。 当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(用 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中,并返回此 String 对象的引用。 它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern() 才为 true。
isEmpty()判断字符串是否为空
public class StringTest5{
    
    public static void main(String[] args){
        String s1 = new String("http://WWW.baidu.com"); 
        String s2="baidu";
        
        //boolean equals(String string) 判断两个字符串是否相等 地址 长度 每个字符 equals只能判断是否相等,而compareTo除了看出是否相等,还等看出大小
        System.out.println("判断两个字符串是否相等");
        String s = new String("xyz");
        System.out.println("xyz".equals(s));
        //true
        
        //boolean equalsIgnoreCase(String string)
        System.out.println("忽略大小写后判断两个字符串是否相等");
        System.out.println("XyZ".equalsIgnoreCase(s));
        //true
        
        //int length()
        System.out.println("获取字符串的长度");
        System.out.println(s2.length());
        //5
        
        //char charAt(int index) 获取字符串对应索引的字符
        System.out.println("获取字符串对应索引的字符");
        char c = s2.charAt(1);
        System.out.println(c);//好
        //a
        
        //indexOf(String string) 判断某个子字符串在字符串上第一次出现处的索引
        System.out.println("判断某个子字符串在字符串上第一次出现处的索引");
        System.out.println(s1.indexOf('.')); 
        //10
        
        //indexOf(String string,int startIndex)判断某个子字符串在字符串上从指定索引startindex开始第一次出现处的索引
        System.out.println("判断某个子字符串在字符串上从指定索引startindex开始第一次出现处的索引");
        System.out.println(s1.indexOf('.', 11));
        //16
        
        //lastindexOf(String string) 返回指定子字符串在此字符串中最后一次出现处的索引。
        System.out.println("返回指定子字符串在此字符串中最后一次出现处的索引。");
        System.out.println(s1.lastIndexOf('.')); 
        //16
        
        //lastindexOf(String str,  int endsIndex) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引endsIndex开始反向搜索。
        System.out.println("返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引endsIndex开始反向搜索。");
        System.out.println(s1.lastIndexOf('.',15)); 
        //10
            
        //boolean contains(String string) 判断前面的字符串是否包含后面的字符串
        System.out.println("判断前面的字符串是否包含后面的字符串");
        System.out.println("helloworld".contains("world"));//true
        System.out.println(s.contains("https://"));//false
        
        //boolean startsWith(String string)   判断当前字符串是否以某个字符串开始
        System.out.println("判断当前字符串是否以某个字符串开始");
        System.out.println(s1.startsWith("https://"));//false
        
        //boolean endsWith(String string)   判断当前字符串是否以某个字符串结尾
        System.out.println("判断当前字符串是否以某个字符串结尾");
        System.out.println("test.txt".endsWith(".java"));//false
        System.out.println("test.txt".endsWith(".txt"));//true
        
        //int comparTo(String string) 
        System.out.println("按照字典顺序比较两个字符串大小");
        int res1 = "abc".compareTo("abc");
        System.out.println(res1);//0  前后一致 10-10 = 0
        int res2 = "abcd".compareTo("abcde");
        System.out.println(res2);//-1 前小后大 9-10 = -1
        int res3 = "abce".compareTo("abcd");
        System.out.println(res3);//1 前大后小 10-9 = 1
        int res4 = "abc".compareTo("bac");
        System.out.println(res4);//-1 两个字符串对应位置的字符依此按照字典顺序比较,分出胜负就不比较了
        
        //toLower()
        System.out.println("将字符串转换为小写");
        System.out.println(s1.toLowerCase());//http://www.baidu.com
        
        //toUpper()
        System.out.println("将字符串转换为大写");
        System.out.println(s1.toUpperCase());//HTTP://WWW.BAIDU.COM
        
        //string subString(int index)
        System.out.println("将字符串从索引index位置截取到结尾");//WWW.baidu.com
        System.out.println(s1.substring(7));
        
        //string subString(int index)
        System.out.println("将字符串从索引startsindex位置截取到索引endsindex位置,前闭后开区间");
        System.out.println(s1.substring(7,10));//WWW
        
        //trim()
        System.out.println("去除字符串前后的空格");
        System.out.println("  xyz  ".trim());//xyz
        
        //replace(String string)
        System.out.println("返回一个新的字符串,它是通过用 `newChar` 替换此字符串中出现的所有 `oldChar` 得到的");
        System.out.println(s1.replace("http://","https://"));//https://WWW.baidu.com
        
        //split(String string)
        System.out.println("按照正则表达式string分割字符");
        String [] time = "2022-5-22".split("-");
        for(int i=0;i<time.length;i++) {
            System.out.println(time[i]);
        }
        /*
         *  2022
            5
            22
         * */
        
        //split(String string,int limit)
        System.out.println("分割,保留末尾的空字符");
        String [] time1 = "2022-5-22   ".split("-",3);
        for(int i=0;i<time1.length;i++) {
            System.out.println(time1[i]);
        }
        /*
         *  2022
            5
            22   
         * */
        
        //join(String s,str1,str2....)
        System.out.println("以s为连接符,连接字符串");
        System.out.println(String.join(".", "www","baidu.com"));//www.baidu.com
        
        //concat(String string) 将指定字符串连接到此字符串的结尾。
        System.out.println("将指定字符串连接到此字符串的结尾。");
        System.out.println(s2.concat(".com"));//baidu.com
        
        //char[] toCharArray 将字符串转换为字符数组
        System.out.println("将字符串转换为字符数组");
        char [] chars=s2.toCharArray();
        for(int i = 0;i<chars.length;i++) {
            System.out.println(chars[i]);
        }
        /*
         *  b
            a
            i
            d
            u
         * */
        //intern() 返回字符串对象的规范化表示形式。
        System.out.println("返回字符串对象的规范化表示形式。");
        System.out.println(s1.intern());//http://WWW.baidu.com
                
        //isEmpty() 判断某个字符串是否为空字符串 数组长度是length属性,字符串长度是length方法
        System.out.println("判断某个字符串是否为空字符串");
        System.out.println(s2.isEmpty());//false        
    }
}
  • 50
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好教员好

您的鼓励是我前进动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值