zoukankan      html  css  js  c++  java
  • 动手动脑(补)

    public class StringPool {

        

    public static void main(String args[])

        {

            

    String s1="a";

            

    String s2=s1;

                    

    System.out.println(s1==s2);//true

                    s1+="b";

    System.out.println(s1==s2);//false       

    System.out.println(s1=="ab");//false

                System.out.println(s1.equals("ab"));//true

    }

    }

    分析:给字符串赋值意味着两个变量现在引用同一个字符串变量对象,所以s1==s2返回值为true,String对象是只读的,使用+修改了s1变量的值,实际上得到了一个新的字符串,内容为ab,与原来s1所引用的对象无关,所以返回值是false,字符串常量引用的字符串与s1引用的对象无关,string.equas()方法可以比较两个字符串的内容。

    public class StringEquals {

        

    /**

         * @param args the command line arguments

         */

        

    public static void main(String[] args) {

            

    String s1=new String("Hello");

            

    String s2=new String("Hello");

            

    System.out.println(s1==s2);//使用new关键字创建了两个不同的对象,所以返回值为false

            

    System.out.println(s1.equals(s2));//内容相同,值为true

            

    String s3="Hello";

            

    String s4="Hello";

              

    System.out.println(s3==s4);//内容相同的常量,引用同一个对象,返回值为true

            

    System.out.println(s3.equals(s4));//内容相同,返回值为true

            

        

    }

    }

    Length()//返回当前字符串长度

    charAt(int index):取字符串中的某一个字符,其中的参数index指的是字符串中的序数。字符串的序数从0到length()-1.

    getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin):该方法将字符串拷贝到字符数组中。其中srcBegin为拷贝的起始位置,srcEnd为拷贝的结束位置,字符串数组dst为目标字符数组,dstBegin为目的字符数组拷贝的起始位置。

    replace(char oldchar,char newchar)//将字符串中的第一个oldchar替换成newchar

    toUpperCase()://将字符串转化成大写

    toLowerCase()://将字符串转化为小写

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

    toCharArray()://将string对象转换成char数组

      

  • 相关阅读:
    生产环境中使用google otp登录不上服务器的解决办法
    kvm虚拟机快照创建与管理
    什么是字节码、机器码、本地代码?
    什么是CDN?哪些是流行的jQuery CDN?使用CDN有什么好处?
    Spring Boot(七):Mybatis 多数据源最简解决方案
    Spring Boot(六):如何优雅的使用 Mybatis
    Spring Boot、Spring MVC 和 Spring 有什么区别?
    Spring Boot(五):Spring Boot Jpa 的使用
    Spring Boot(四):Thymeleaf 使用详解
    Spring Boot(三):Spring Boot 中 Redis 的使用
  • 原文地址:https://www.cnblogs.com/liying123/p/4909608.html
Copyright © 2011-2022 走看看