Java基础_常用类_String

public class StringTest1 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "hello";
        //"hello"存在于data segment数据区,编译器会对数据区进行优化,已经存在的字符串常量,不会在重新分配内存存储,所以s1,s3指向数据区中的同一个字符串常量
        System.out.println(s1 == s3);//true
        s1 = new String("hello");
        s2 = new String("hello");
        //这里新建了两个字符串对象,虽然两个字符串的对象的内容都指向数据区中的同一个字符串常量,但是s1,s2指向的却是堆中的两个不同的String对象
        System.out.println(s1 == s2);//false
        //String对象中的equals方法重写了Object中的equals方法
        /*
         * public boolean equals(Object anObject)比较此字符串与指定的对象。当且仅当该参数不为 null,并且是表示与此对象相同的字符序列的 String 对象时,结果才为 true。
            
            覆盖:
            类 Object 中的 equals
            参数:
            anObject - 与此 String 进行比较的对象。
            返回:
            如果 String 相等,则返回 true;否则返回 false。
         */
        System.out.println(s1.equals(s2));//true
        
        char c[] = {'s', 'u', 'n', ' ', 'j', 'a', 'v', 'a'};
        String s4 = new String(c);
        /*
         * String
            public String(char[] value,
                          int offset,
                          int count)分配一个新的 String,它包含来自该字符数组参数的一个子数组的字符。offset 参数是子数组第一个字符的索引,count 参数指定子数组的长度。该子数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串。
            
            参数:
            value - 作为字符源的数组。
            offset - 初始偏移量。
            count - 长度。
         */
        String s5 = new String(c, 4, 4);
        System.out.println(s4);//sun java
        System.out.println(s5);//java
    }
}


常用方法

public char charAt(int index) 返回字符串中第index个字符,从0开始

public int length() 返回字符串长度,注意区分数组中的length属性

public int indexOf(String str) 返回字符串中出现子串str的第一个位置

public int indexOf(String str, int formIndex) 返回字符串中从fromIndex开始出现str的第一个位置

public boolean equalsIgnoreCase(String another) 比较字符串与another是否一样(忽略大小写)

public String replace(char oldChar, char newChar) 在字符串中用newChar字符替换oldChar字符

public class StringTest2 {
    public static void main(String[] args) {
        String s1 = "sun java";
        String s2 = "Sun Java";
        System.out.println(s1.charAt(1));//u
        System.out.println(s2.length());//8
        System.out.println(s1.indexOf("java"));//4
        System.out.println(s1.indexOf("Java"));//-1,没有找到
        System.out.println(s1.equals(s2));//false
        System.out.println(s1.equalsIgnoreCase(s2));//true
        String s = "我是程序员,我在学习java";
        String sr = s.replace('我', '你');
        System.out.println(sr);//你是程序员,你在学java
    }
}

public boolean startsWith(String prefix) 判断字符串是否以prefix字符串开头

public boolean endsWith(String suffix) 判断字符串是否以prefix字符串结尾

public String toUpperCase() 返回一个字符串为该字符串的大写形式

public String toLowerCase() 返回一个字符串为该字符串的小写形式

public  String substring(int beginIndex) 返回该字符串从beginIndex开始到结尾的子字符串

public String substring(int beginIndex, int endIndex) 返回该字符串从beginIndex开始到endIndex结尾的子字符串

public String trim() 返回将该字符串去掉开头和结尾空格后的字符串

public class StringTest3 {
    public static void main(String[] args) {
        String s = "Welcome to Java World!";
        String s1 = "  sun java  ";
        System.out.println(s.startsWith("Welcome"));//true
        System.out.println(s.endsWith("World"));//false
        String sL = s.toLowerCase();
        String sU = s.toUpperCase();
        System.out.println(sL);//welcome to java world!
        System.out.println(sU);//WELCOME TO JAVA WORLD!
        String subS = s.substring(11);//Java World!
        String sp = s1.trim();
        System.out.println(sp);//sun java
        
    }
}

public static String valueOf(...) 可以将基本数据类型转换为字符串

public String[] split(String regex) 可以将一个字符串按照指定的分隔符分割,返回分割后的字符串数组

public class StringTest4 {
    public static void main(String[] args) {
        int j = 1234567;
        String sNumber = String.valueOf(j);
        System.out.println(sNumber + "是" + sNumber.length() + "位数。");//1234567是7位数。
        String s = "Mary, F, 1976";
        String[] strSplit = s.split(",");
        for(int i=0; i<strSplit.length; i++) {
            System.out.println(strSplit[i]);
        }

        /*
         *   Mary
             F
             1976
         */

        System.out.println(String.valueOf(true));//true
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值