Java常用类 - String类详解

Java常用类——String类

  • 创建字符串:每个双引号括起来的字符串常量都是String类的一个实例;也可以new。

  • 连接字符串:”+“;string1.concat(string2)。

    public class Demo01 {
        public static void main(String[] args) {
            String s1 = "abc";
            String s2 = new String("abc");
            // + 两边只要有一个是字符串,即作为字符串连接符
            String s3 = "abc" + 123;
            String s4 = 23 + "12";
            String s5 = "ano";
    
            System.out.println(s3); //abc123
            System.out.println(s4); //2312
            System.out.println(s5.concat(s1)); //anoabc
        }
    }
    
  • String类和常量池

    常量池中存放的内容是在类加载完成后存到String Pool中的,在每个JVM中只有一份,存放的是字符串常量的引用值(在堆中生成字符串对象实例)。

    public class Demo02 {
        public static void main(String[] args) {
            String a1 = "hello";
            String a2 = "hello";
            String a3 = new String("hello");
            System.out.println(a1 == a2);  //true
            System.out.println(a1 == a3);  //false
        }
    }
    
  • String类是不可变对象。位于java.lang包中。

    String类对象代表不可变的Unicode字符串序列,因此String对象是不可变对象。字符串内容全部存储在value[]中,而变量value是final类型的。

  • String subString(beginIndex, endIndex); 截取完后返回一个新的字符串。

  • String字符串内容的比较用boolean equals()。

    public class Demo03 {
        public static void main(String[] args) {
            String b1 = "good luck";
            String b2 = "good" + " luck";
            System.out.println(b1 == b2); //true
    
            String b3 = "good";
            String b4 = " luck";
            String b5 = b3 + b4;
            System.out.println(b1 == b5); //false
            System.out.println(b1.equals(b5)); // true
        }
    }
    
  • String类的常用方法

方法说明
char charAt(int index)返回字符串处第index处的 char 值
boolean equals(String other)判断字符串内容是否相等
boolean equalsIgonreCase(String other)判断字符串内容是否相等(忽视大小写)
int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
int lastIndexOf(String str)返回指定子字符串在此字符串中最右边出现处的索引
int length()返回此字符串的长度
String replace (char oldChar, char newChar)返回一个新的字符串,通过用 newChar 替换此字符串中出现的所有 oldChar 得到
boolean startsWith(String prefix)测试此字符串是否以指定的前缀开始
boolean endsWith(String suffix)测试此字符串是否以指定的后缀结束
String subString(int beginIndex)返回一个新的字符串,从beginIndex到最后
String subString(int beginIndex, int endIndex)返回一个新的字符串,从beginIndex到endIndex-1的
String toLowerCase()返回一个新的字符串,将此 String 中的所有字符都转换为小写
String toUpperCase()返回一个新的字符串,将此 String 中的所有字符都转换为大写
String trim()返回字符串的副本,忽略前导空白和尾部空白
public class Demo04 {
    public static void main(String[] args) {
        String s1 = "Hello Ano";
        String s2 = "hello ano";
        String s3 = " Hello AnoBabe  ";
        System.out.println(s1.charAt(4)); //o

        System.out.println(s1.equals(s2)); //false
        System.out.println(s1.equalsIgnoreCase(s2)); //true

        System.out.println(s1.indexOf("An"));  //6
        System.out.println(s1.indexOf("an"));  //不存在返回-1
        System.out.println(s1.lastIndexOf("Ano")); //6

        System.out.println(s1.length());  //9

        System.out.println(s1.replace("Ano", "Babe"));  //Hello Babe

        System.out.println(s1.startsWith("H"));
        System.out.println(s1.endsWith("no"));

        System.out.println(s1.substring(1,4)); //ell
        System.out.println(s1.toLowerCase()); //hello ano
        System.out.println(s2.toUpperCase()); //HELLO ANO

        System.out.println(s3.trim());
        System.out.println(s3);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值