• API_String的重要特性


    不能被继承

     

     

     

     

     

     

     

    1. package API.Test;
    2. import org.junit.Test;
    3. /*
    4. 一、java.lang.String 类:
    5. String str1="abc";
    6. String str2=new String("abc");
    7. 二者区别:
    8. str1:代表一个对象,至少在内存中开辟一块内存空间(栈中开辟存放对象的引用,“xxx”在字符串池中查找是否有,没有则开辟,反之不需要)
    9. str2:代表一个对象,至少在内存中开辟两块内存空间
    10. * */
    11. public class StringTest {
    12. @Test
    13. public void test() {
    14. String str1 = "abc";
    15. String str2 = new String("abc");
    16. System.out.println(str1 == str2);//false
    17. String str3 = "abc";
    18. System.out.println(str1 == str3);
    19. System.out.println(str2 == str3);
    20. System.out.println(str3 == str2.intern());
    21. }
    22. }

     string:不可变的字符序列

     

     

     

     

    二、String类的常用方法:
        1.获取字符串的方法:
        ①.String concat (String str):串接字符串
        ②String substring(int beginIndex):获取取字符串的子串
        String substring(int beginIndex,endIndex):包含头不包含尾
        ③String toLowerCase()和String toUpperCase():转换为小写/大写
        ④String trim():删除首尾空格或制表符

     

     

    2.搜索方法:
    ①int indexOf(int ch) :获取指定字符在字符串中的位置,若没有指定的字符,返回int indexOf (int ch,int fromIndex)
    :从指定位置开始搜索
    int indexOf (String str)
    int indexOf (String str, int fromIndex)
    int lastIndexOf (int ch):反向获取指定字符位置

    构成重载:

     

     

     

     

    3.判断方法:
    ①boolean equals (0bject obj):判断是否相等,严格区分大小写
    boolean equalsIgnoreCase(String str):判断是否相等,不考虑大小写
    ②boolean contains (String str):判断是否包含某字符串
    ③boolean startsWith(String str)和boolean endsWith(String str):判断是否已指定字符串开始/结尾
    ④boolean isEmpty():判断字符串是否为空
    

     

     4、其他方法

    ①length():返回字符串长度

    ②char charAt(int index):返回索引处的字符

    ③将字符数组转换为字符串

            构造器:

                            String(char[] ch)

                            String(char[] ch,offset,count):将数组中一部分转换为字符串

            静态方法:

                            static String copyValueOf(char[] ch)

                            static String copyValueOf(char[] ch,offset,count)

                            static String ValueOf(char[])

            将字符串转换为字符数组:char[] toCharArray()

    ④String replace(char oldCahr, char newCahr) :替换字符串中字符
        String replace(String oldStr,String oldStr):替换字符串中字符串

    ⑤String[] split (String r):根据指定符号切割
     

     

     

     

     

  • 相关阅读:
    基于最小二乘插值(Least-Squares Interpolation)图像超分辨率重构算法研究-附Matlab代码
    深度学习入门(五十二)计算机视觉——风格迁移
    预训练模型之ELMO -《Deep contextualized word representations》论文笔记 + 高频面试题
    Mysql 性能优化就是这么简单,大佬带你深入浅出
    Spring MVC(中)
    工业互联网行至深水区,落地的路要怎么走?
    笔试题:金额拆分
    什么是机器学习特征工程?【数据集特征抽取(字典,文本TF-Idf)、特征预处理(标准化,归一化)、特征降维(低方差,相关系数,PCA)】
    04 MIT线性代数-矩阵的LU分解 Factorization into A=LU
    【数据结构】链表栈
  • 原文地址:https://blog.csdn.net/m0_54397364/article/details/126419876