• 学习java第40天


    1.StringBuilder和StringBuffer用法一致,但是StringBuilder线程不安全,速度较快

    2.StringBuilder和String的转换

    public class StringBuilderTest1 {
        public static void main(String[] args) {
            StringBuilder strBui = new StringBuilder();
            strBui.append("hello").append("world");

            String str = strBui.toString();
            System.out.println("StringBuilder转换为String输出:"+str);

            StringBuilder strBui1 = new StringBuilder(str);
            System.out.println("String转换为StringBuilder输出:"+strBui1);
        }
    }

    //StringBuilder转换为String输出:helloworld

    //String转换为StringBuilder输出:helloworld

    3.StringBuilder的拼接,反转

    public class StringBuilderTest2 {
        public static void main(String[] args) {
            StringBuilder strBui = new StringBuilder();
            strBui.append(hello).append("abc").append(123);   //拼接
            System.out.println(strBui);
            System.out.println(strBui.reverse());                         //反转
        }
    }

    // helloabc123

    //321cbaolleh

    4.字符串是否对称

    public class StringBuilderTest3 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入一个字符串:");
            String str = sc.nextLine();
            boolean b = isSymmetry(str);
            System.out.println("是否对称:"+b);
        }
        public static boolean isSymmetry(String str) {
            StringBuilder strBui = new StringBuilder(str);
            strBui.reverse();
            String strRes = strBui.toString();
            return strRes.equals(str);
        }
    }

    //请输入一个字符串:abc

    //是否对称:false

    5.明天学习内容:字符串的编码

    .

  • 相关阅读:
    Django多表操作
    Django单表操作
    Django模板语言的复用
    DTL
    Django视图层、虚拟环境
    Django路由详解
    初识Django
    web框架
    JQ初级
    linux的基础配置命令
  • 原文地址:https://www.cnblogs.com/SirNie/p/13503763.html
Copyright © 2020-2023  润新知