• 动手动脑(补)


    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数组

      

  • 相关阅读:
    spring开发_Spring+Hibernate_HibernateDaoSupport
    java开发_STMP邮箱客户端_发送邮件
    struts2开发_userlogin_模拟用户登录
    spring开发_邮箱注册_激活_获取验证码
    MFC笔记(1)
    MFC笔记(2)菜单
    wpf控件开发基础(5) 依赖属性实践
    wpf控件开发基础(3) 属性系统(2)
    wpf控件开发基础(2) 属性系统(1)
    Caliburn笔记依赖注入容器(wpf框架)
  • 原文地址:https://www.cnblogs.com/liying123/p/4909608.html
Copyright © 2020-2023  润新知