zoukankan      html  css  js  c++  java
  • java学习日记 StringBuffer类

    1、与String区别1

    String用+连接字符

    StringBuffer用append()方法

    String不能修改

    StringBuffer可修改

    public class StringBufferDemo1 {
        public static void main(String[] args) {
            StringBuffer sbu = new StringBuffer();
            sbu.append("Hello").append("World").append("!!!");
            System.out.println("修改之前:"+sbu);
            System.out.println("修改之后:"+change(sbu));
    
        }
        public static StringBuffer change(StringBuffer temp){
            return temp.append("jxqwc").append("gaile");
        }
    }

    运行结果:

    修改之前:HelloWorld!!!
    修改之后:HelloWorld!!!jxqwcgaile

    2、两种方法将String类转换成StringBuffer类

    public class StringBufferDemo1 {
        public static void main(String[] args) {
            StringBuffer sbu = new StringBuffer("Hello");
            System.out.println(sbu);
           }
    }
    public class StringBufferDemo1 {
        public static void main(String[] args) {
            StringBuffer sbu = new StringBuffer();
            sbu.append("Hello");
            System.out.println(sbu);
        }
    }

    3、将StringBuffer类转换成String类

    public class StringBufferDemo1 {
        public static void main(String[] args) {
            StringBuffer sbu = new StringBuffer("Hello");
            String str = sbu.toString();
            System.out.println(sbu);
        }
    }

  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/cathycheng/p/13204267.html
Copyright © 2011-2022 走看看