String类-常用构造方法-常用方法

String类中的构造方法。
 *  第一个:String s = new String("");
 *  第二个:String s = ""; 最常用
 *  第三个:String s = new String(char数组);
 *  第四个:String s = new String(char数组,起始下标,长度);
 *  第五个:String s = new String(byte数组);
 *  第六个:String s = new String(byte数组,起始下标,长度)

public class StringTest04 {
    public static void main(String[] args) {

        // 创建字符串对象最常用的一种方式
        String s1 =  "hello world!";
        // s1这个变量中保存的是一个内存地址。但是输出一个字符串,说明String类已经重写了toString()方法。
        System.out.println(s1);//hello world!

        // 常用的构造方法。
        byte[] bytes = {97, 98, 99}; // 97是a,98是b,99是c
        String s2 = new String(bytes);

        // 输出一个引用的时,自动调用toString()方法,默认Object的话,会自动输出对象的内存地址。
        System.out.println(s2); //abc

        // String(字节数组,数组元素下标的起始位置,长度) 将byte数组中的一部分转换成字符串。
        String s3 = new String(bytes, 1, 2);
        System.out.println(s3); // bc

        // 将char数组全部转换成字符串
        char[] chars = {'','','','',''};
        String s4 = new String(chars);
        System.out.println(s4);
        // 将char数组的一部分转换成字符串
        String s5 = new String(chars, 2, 3);
        System.out.println(s5);

        String s6 = new String("helloworld!");
        System.out.println(s6); //helloworld!
    }
}

String 类常用方法

1、char c  =  charAt(int index) 返回指定索引出 char值 

2、int result  = "asd" .compareTo("asd")  按照字典顺序比较字符串

3、boolean contains();  判断前面的字符串中是否包含后面的子字符串。System.out.println("HelloWorld.java".contains(".java"));  //true

4、boolean endsWith();判断前面的字符串以什么结尾;System.out.println("test.txt".endsWith(".java"));  //true

5、boolean equals();判断两个字符串是否相等:System.out.println("abc".equals("abc")); // true

6、boolean equalsIgnoreCase(); 忽略大小写判断字符串是否相等:System.out.println("abc".equals("abc")); // true

7、byte[] bytes = "asd" .getBytes();  将字符串转化为byte数组

8、int  intdexOf()   判断某个子字符串在当前字符串中第一次出现的索引:System.out.println("oraclejavac".indexOf("java")); // 6

9、boolean isEmpty(); 判断某个字符串是否为空

10、int  legth(); 判断字符串的长度  :System.out.println("abc".length()); // 3

11、int lastIndexOf();  判断某个子字符串最后一在当亲字符串中出现的索引:System.out.println("oraclejava".lastIndexOf("java")); //6

12、String replace();   将当前字符串中的子字符串,替换为括号中新的字符串:String newStr= "http://www.baidu.com".replace("http://", "https://")

13、String[] arr = "字符串对象".split(); 将当前字符串对象以“-”分割:String[] ymd = "1980-10-11".split("-");

14、boolean startWiths(); 判断当前子字符是否以某个字符串开头:System.out.println("http://www.baidu.com".startsWith("http")); // true

15、subString(开始位置下标);  截取字符串:System.out.println("http://www.baidu.com".substring(7));//结果:www.baidu.com

16、sunString(开始位置下标,结束位置下标(不包含在内));截取字符串:System.out.println("http://www.baidu.com".substring(7, 10)); //www

17、char[] array = "字符串". toCharArray();   将字符串转化为char数组;

18、toLowerCase();  将字符串转为小写

19、toUpperCase();将字符串转为大写

20、trim();   去除字符串中的空白

21、String.valueOf() 唯一个静态方法:将非字符串,转为字符串

public class StringTest05 {
    public static void main(String[] args) {

        //1  .char charAt(int index)返回指定索引出char值
        // "中国人"是一个字符串String对象。只要是对象就能“点.”
        char c = "中国人".charAt(1); 
        System.out.println(c); //// 2 .int compareTo(String anotherString) 按字典顺序比较字符串
        // 字符串之间比较大小不能直接使用 > < ,需要使用compareTo方法。
        int result = "abc".compareTo("abc");
        System.out.println(result); //0(等于0) 前后一致  8 - 8 = 0

        int result2 = "abcd".compareTo("abce");
        System.out.println(result2); //-1(小于0) 前小后大 8 - 9 = -1

        int result3 = "abce".compareTo("abcd");
        System.out.println(result3); // 1(大于0) 前大后小 9 - 8 = 1

        // 拿着字符串第一个字母和后面字符串的第一个字母比较。能分胜负就不再比较了。
        System.out.println("xyz".compareTo("yxz")); // -1

        // 3 .boolean contains(CharSequence s)是否包含某个字符串
        // 判断前面的字符串中是否包含后面的子字符串。
        System.out.println("HelloWorld.java".contains(".java")); // true
        
        // 4  .boolean endsWith(String suffix) 判断字符串以什么结尾
        // 判断当前字符串是否以某个子字符串结尾。
        System.out.println("test.txt".endsWith(".java")); // false
     
        // 5   .boolean equals(Object anObject) 比较两个字符串使用equals方法
        // equals只能看出相等不相等。compareTo方法可以看出是否相等,并且可以看出谁大谁小。
        System.out.println("abc".equals("abc")); // true

        // 6  .boolean equalsIgnoreCase(String anotherString) 忽略大小写判断两个字符串是否相等
        System.out.println("ABc".equalsIgnoreCase("abC")); // true

        // 7    .byte[] bytes = getBytes(); 将字符串对象转换成字节数组
        byte[] bytes = "abcdef".getBytes();
        for(int i = 0; i < bytes.length; i++){
            System.out.println(bytes[i]);
        }//97-98-99-100-101-102

        // 8  .int indexOf(String str)判断某个子字符串在当前字符串中第一次出现处的索引。
        System.out.println("oraclejavac".indexOf("java")); // 6

        // 9  .boolean isEmpty() 判断某个字符串是否为“空字符串”。底层源代码调用的应该是字符串的length()方法。
        String s = "a";
        System.out.println(s.isEmpty());

        // 10  .int length()
        // 判断题:判断数组长度和判断字符串长度不一样
        // 判断数组长度是length属性,判断字符串长度是length()方法。
        System.out.println("abc".length()); // 3
        System.out.println("".length()); // 0

        // 11  .int lastIndexOf(String str)判断某个字符串在当前字符串中最后一次出现的索引
        System.out.println("oraclejava".lastIndexOf("java")); //6

        // 12  .String replace(CharSequence target, CharSequence replacement)
        // 替换。String的父接口就是:CharSequence
        String newStr= "http://www.baidu.com".replace("http://", "https://")   
        System.out.println(newStr); //https://www.baidu.com
        
        // 把以下字符串中的“=”替换成“:”
        String newStr2 = "name=zhangsan&password=123&age=20".replace("=", ":");
        System.out.println(newStr2); //name:zhangsan&password:123&age:20

        // 13  .String[] arr= split(String regex)  拆分正则表达式(字符串)
        String[] ymd = "1980-10-11".split("-"); //"1980-10-11"以"-"分隔符进行拆分。
        for(int i = 0; i < ymd.length; i++){
            System.out.println(ymd[i]);
        }//结果:1980   10   11

        // 14  .boolean startsWith(String prefix) 判断某个字符串是否以某个字符串开始。
        System.out.println("http://www.baidu.com".startsWith("http")); // true

        // 15 String substring(int beginIndex) 参数是起始下标。截取字符串
        System.out.println("http://www.baidu.com".substring(7));//www.baidu.com

        // 16  String substring(int beginIndex, int endIndex)
        // beginIndex起始位置(包括); 
        // endIndex结束位置(不包括)
        System.out.println("http://www.baidu.com".substring(7, 10)); //www

        // 17  char[] arr = toCharArray()  将字符串转换成char数组
        char[] chars = "我是中国人".toCharArray();
        for(int i = 0; i < chars.length; i++){ System.out.println(chars[i]);}

        // 18 String toLowerCase()  转换为小写。
        System.out.println("ABCDefKXyz".toLowerCase());

        // 19  String toUpperCase();
        System.out.println("ABCDefKXyz".toUpperCase());

        // 20  String trim(); 去除字符串前后空白
        System.out.println("     hello   world   ".trim());

        // 21  String中只有一个方法是静态的。(使用类名调用String.valueOf())
        // valueOf(); 作用:将“非字符串”转换成“字符串”
        //String s1 = String.valueOf(true);

        String s1 = String.valueOf(new Customer());
        // 静态的valueOf()方法,参数是一个对象的时候,会自动调用该对象的toString()
        //System.out.println(s1); // 没有重写toString()方法之前是对象内存地址          com.bjpowernode.javase.string.Customer@10f87f48
        System.out.println(s1); //重写toString方法之后之后:我是一个VIP客户!!!!
          
        
        //println 底层调用了String.valueOf()方法;String类的静态方法 
        // 通过源代码可以看出:为什么输出一个引用的时候,会调用toString()方法!!!!
        // 本质上System.out.println()这个方法在输出任何数据的时候都是先转换成字符串,再输出。

    }
}

//创建一个Object对象
public class Customer {
    // 重写toString()方法
    @Override
    public String toString() {
        return "我是一个VIP客户!!!!";
    }
}

 

posted @ 2022-05-01 18:57  280887072  阅读(299)  评论(0编辑  收藏  举报