zoukankan      html  css  js  c++  java
  • String类

    1、String的不可变性

    import org.junit.Test;
    
    public class test01 {
        /*
        String:字符串,使用一对""引起来表示。
        1. String声明为final,不可被继承。
        2. String实现了Serializable接口:表示字符串是支持序列化的。
           String实现了Comparable接口:表示String可以比较大小。
        3. String内部定义了final char[] value用于存储字符串数据
        4. String代表不可变的字符序列。简称:不可变性。
        5. 通过字面量的方式(区别于new)给一个字符串赋值,此时的字符串值声明在字符串常量池中。
         */
        @Test
        public void testString(){
            String s1 = "abc";
            String s2 = "abc";
            System.out.println(s1 == s2);//true
    
            s2 += "qwe";
            System.out.println(s1 == s2);//false
    
            String s3 = "abc";
            String s4 = "abc";
            s4 = s3.replace("a", "m");
            System.out.println(s3 == s4);//false
        }
    }
    
    import org.junit.Test;
    
    public class test03 {
        /*
        结论:
        1.常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量。
        2.只要其中有一个是变量,结果就在堆中。
        3.如果拼接的结果调用intern()方法,返回值就在常量池中。
         */
        @Test
        public void testString(){
            String s1 = "javaEE";
            String s2 = "hadoop";
    
            String s3 = "javaEEhadoop";
            String s4 = "hadoop";
            String s5 = s1 + "hadoop";
            String s6 = "javaEE" + s2;
            String s7 = s1 + s2;
    
            System.out.println(s3 == s4);//true
            System.out.println(s3 == s5);//false
            System.out.println(s3 == s6);//false
            System.out.println(s3 == s7);//false
            System.out.println(s5 == s6);//false
            System.out.println(s5 == s7);//false
            System.out.println(s6 == s7);//false
    
            String s8 = s5.intern();//返回值d得到的s8使用的是常量池中已经存在的"javaEEhadoop"
            System.out.println(s3 == s8);//true
        }
    }
    

    2、String的实例化方法

    import org.junit.Test;
    
    public class test02 {
        /*
        String的实例化方法:
        方式一:通过字面量定义的方式
        方式二:通过new + 构造器的方式
    
        面试题:String s = new String("abc");方式创建对象,在内存中创建了几个对象?
                两个:一个是堆空间中new结构,另一个是char[]对应的常量池中的数据"abc"
         */
        @Test
        public void testString(){
            //通过字面量定义的方式,此时的s1和s2的数据abc声明在方法区中的字符串常量池中。
            String s1 = "abc";
            String s2 = "abc";
    
            //通过new + 构造器的方式:此时s3和s4保存的地址值,是数据在堆空间中开辟空间以后对应的地址
            String s3 = new String("abc");
            String s4 = new String("abc");
    
            System.out.println(s1 == s2);//true
            System.out.println(s1 == s3);//false
            System.out.println(s1 == s4);//false
            System.out.println(s3 == s4);//false
        }
    }
    

    3、String 常用函数

    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(不包含)的一个子字符串
    
    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
    注:当indexOf(str)和lastIndexOf(str)返回值相同时,要么存在唯一的一个str,要么不存在str。
    
    替换:
    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个,如果超过了,剩下的全部都放到最后一个元素中。
    

    4、String与其他基本数据类型的转换

    import org.junit.Test;
    
    import java.io.UnsupportedEncodingException;
    import java.util.Arrays;
    
    public class test07 {
        /*
        String 与 char[]之间的转换
    
        String --> char[]:调用String的toCharArray()
        char[] --> String:调用String的构造器
         */
        @Test
        public void testString01(){
            String str1 = "abc123";
    
            char[] charArray = str1.toCharArray();
            for(int i = 0; i < charArray.length; i++){
                System.out.println(charArray[i]);
            }
    
            char[] arr = new char[]{'h', 'e', 'l', 'l', 'o'};
            String str2 = new String(arr);
            System.out.println(str2);
        }
    
        
        /*
        复习:
        String 与基本数据类型、包装类之间的转换
    
        String --> 基本数据类型、包装类:调用包装类的静态方法:parseXxx(str)
        基本数据类型、包装类 --> String:调用String重载的valueOf(Xxx)
         */
        @Test
        public void testString02(){
            String str1 = "123";
    
            int num = Integer.parseInt(str1);
    
            String str2 = String.valueOf(num);
            String str3 = num + "";
    
        }
    
        
        /*
        String 与 byte[]之间的转换
        编码:String --> byte[]:调用String的getBytes()
        解码:byte[] --> String:调用String的构造器
    
        说明:解码时,要求解码使用的字符集必须和编码时一致,否则乱码。
         */
        @Test
        public void testString03() throws UnsupportedEncodingException {
            String str1 = "abc123中国";
            byte[] bytes = str1.getBytes();//使用默认的字符集进行转换
            System.out.println(Arrays.toString(bytes));
    
            byte[] gbks = str1.getBytes("gbk");//使用gbk字符集进行编码
            System.out.println(gbks);
    
            System.out.println("*********************");
    
            String str2 = new String(bytes);//使用默认的字符集进行解码
            System.out.println(str2);
    
            String str4 = new String(gbks, "gbk");
            System.out.println(str4);//没有出现乱码,编码集和解码集一致。
        }
    }
    
  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/xiaoran991/p/12520140.html
Copyright © 2011-2022 走看看