JAVA常用类之String

 

String的特性

 

 

  1. final class String 可以看出来 String 是一个final类,表示不可变的字符序列;
  2. 字符串是常量,用双引号括起来表示 例如 String str = "hao";
  3. String字符串内容是存在字符数组  char value[] 中的;
  4. String实现了Serializable接口,表示字符串是支持序列化的;
  5. 实现了Comparable接口:表示String可以比较大小

String对象的创建

  1. String str = "hao";
    
    
  2. public String() {
        this.value = "".value;
    }
  3. public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }
  4. public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }
  5. public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count <= 0) {
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            if (offset <= value.length) {
                this.value = "".value;
                return;
            }
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }
  6. 等等一些常用的构造方法进行创建在java.lang.String类中可以查看;
  7. String str = "" 会在常量池中创建空的一个char value[] 字符数组,创建了一个对象
  8. String str = new String();会创建两个对象,一个是常量池中的char value[]对象,一个是new String()的对象,值是引用常量池中的char value[];

字符串对象如何如何存储的

  1. /**
     * 对象的堆地址值不相等
     * 常量拼接常量还是存储在常量池中
     * 常量拼接变量最终存储在堆中
     * 变量拼接变量也是存储在堆中
     * intern会将存储在堆中的返回在常量池中
     */
    
  2. String str1 = "hao";
    String str2 = "hao";
    String str3 = new String("hao");
    String str4 = new String("hao");
    String str5 = str1 ;
    System.out.println(str1 == str2);//true
    System.out.println(str1 == str3);//false
    System.out.println(str3 == str4);//false
    System.out.println(str5 == str1);//true
    System.out.println("----------");
    String s1 = "hao";
    String s2 = "hao";
    String s3 = "haohao";
    String s4 = s1 +s2;
    String s5 = "hao"+"hao";
    String s6 = new String("haohao");
    String s7 = s4.intern();
    System.out.println(s3 == s4);//false
    System.out.println(s3 == s5);//true
    System.out.println(s3 == s6);//false
    System.out.println(s4 == s6);//false
    System.out.println(s5 == s6);//false
    System.out.println(s5 == s7);//false

String类常用方法

  1. //    boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
    //    boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
    //    boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
    //
    //    boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true
    //    int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
    //    int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
    //    int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
    //    int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
    //
    //     indexOf和lastIndexOf方法如果未找到都是返回-1
    
        @Test
        public void test2(){
            String str = "haohaohao";
            boolean h = str.startsWith("h");
            boolean h1= str.endsWith("h");
            boolean h2= str.startsWith("h",0);
            int h3= str.indexOf("h");
            int h4= str.indexOf("h",2);
            int h5= str.lastIndexOf("h");
            int h6= str.lastIndexOf("h",2);//从后往前找,反向索引
            int h7= str.indexOf("g");
            int h8= str.lastIndexOf("g");
    
            System.out.println("测试此字符串是否以指定的h开始:"+h);//true
            System.out.println("测试此字符串是否以指定的后缀结束:"+h1);//false
            System.out.println("测试此字符串从指定索引开始的子字符串是否以指定前缀开始:"+h2);//false 索引是从0开始计算
            System.out.println("返回指定子字符串在此字符串中第一次出现处的索引:"+h3);//
            System.out.println("返回指定子字符串在此字符串中第一次出现处的索引:"+h4);//
            System.out.println("返回指定子字符串在此字符串中第一次出现处的索引:"+h5);//
            System.out.println("返回指定子字符串在此字符串中第一次出现处的索引:"+h6);//
            System.out.println("indexof没有找到的值是:"+h7);
            System.out.println("lastIndexOf没有找到的值是:"+h8);
        }
  2. int length():返回字符串的长度: return value.length
    char charAt(int index): 返回某索引处的字符return value[index]
    boolean isEmpty():判断是否是空字符串:return value.length == 0
    String toLowerCase():使用默认语言环境,将 String 中的所有字符转换为小写
    String toUpperCase():使用默认语言环境,将 String 中的所有字符转换为大写
    String trim():返回字符串的副本,忽略前导空白和尾部空白
    boolean equals(Object obj):比较字符串的内容是否相同
    boolean equalsIgnoreCase(String anotherString):与equals方法类似,忽略大小写
    String concat(String str):将指定字符串连接到此字符串的结尾。 等价于用“+”
    int compareTo(String anotherString):比较两个字符串的大小
    String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串。
    String substring(int beginIndex, int endIndex) :返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串。
  3. 替换:
    String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
    String replace(CharSequence target, CharSequence replacement):使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
    String replaceAll(String regex, String replacement):使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
    String replaceFirst(String regex, String replacement):使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
    匹配:
    boolean matches(String regex):告知此字符串是否匹配给定的正则表达式。
    切片:
    String[] split(String regex):根据给定正则表达式的匹配拆分此字符串。
    String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中。

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值