zoukankan      html  css  js  c++  java
  • java===String,StringBuffer,StringBuilder

    package cn.china;
    
    /**String类的特点:
     * 字符串对象一旦被初始化,就不会被改变。*/
    public class StringDemo1 {
    
        public static void main(String[] args) {
            stringconstruotorDemo();
            StringR();
            Stringa();
        }
    
        public static void stringconstruotorDemo() {
            //String类构造函数应用:特点只针对byte与char型数组
            byte[]arr={97,66,67,68,102};
            String s1=new String(arr);//将数组转换成字符串类型
            System.out.println(s1);
            
            char[]arr1={'a','d','v','i','e','h'};
            String s2=new String(arr1, 1, 2);//从角标1开始取后2位
            System.out.println(s2);
            
        }
    
        private static void Stringa() { 
            // TODO Auto-generated method stub
            String s = "abc";//创建一个字符串对象在常量池中。
            String s1=new String("abc");//创建两个对象,一个new,一个字符串对象在堆内存中。
            System.out.println("s="+s);
            System.out.println("s1="+s1);
            System.out.println(s==s1);//比较地址值
            System.out.println(s.equals(s1));//比较字符串内容,意味着String类中的equals复写了Object类中的equals方法;
        }
    
        public static void StringR() {
            // TODO Auto-generated method stub
            String s="abc";
            s="nba";
            String s1="abc";
            System.out.println(s==s1);//true,原因字符串建立时,会将字符串存放在字符串常量池,而再建立相同字符串的时候会先去常量池去找,如果有直接拿来用;
            
        }
    
    }
    package cn.china;
    /**字符串功能分类
     * "abcd"
     * 1、获取:
     * 获取字符串中字符的个数(长度)。
     *  int length();
     * 根据位置获取字符。
     *  char charAa(int index)
     * 根据字符获取在字符串中第一次出现的位置。
     *  int indexOf(int ch)
     *  int indexOf(int ch,int fromIndex)从指定位置开始ch字符查找
     *  int indexOf(string str)
     *  int indexOf(string str,int fromIndex)
     *  根据字符获取字符串中最后一次出现的位置
     *  int lastIndexOf(int ch)
     *  int lastIndexOf(int ch,int fromIndex)从指定位置开始ch字符查找
     *  int lastIndexOf(string str)
     *  int lastIndexOf(string str,int fromIndex)
     *  获取字符串中一部分字符串,也叫字串;
     *      String substring(int beginIndex, int endIndex)不包含endIndex
     *      substring(int beginIndex)
     * 转换功能:
     * 将字符串变成字符串数组(字符串的切割)
     *  String[]  split(String regex);涉及到正则
     * 将字符串编程字符数组。
     *   char[]  toCharArray();
     * 将字符串转成字节数组。
     *    byte[]  getBytes();
     * 将字符串中的字母转成大小写。
     *    String  toUpperCase();大写
     *    String  toLowerCase();小写
     * 将字符串中的内容进行替换
     *    String replace(char oldChar,char newChar);
     *    String replace(String oldStr,String newStr);
     * 去除字符串左右两边空格
     *  String trim();
     * 将字符串进行连接
     *   String concat(String str);
     * 3、字符串判断功能
     * 两个字符串内容是否相同
     *  boolean equals(Object obj);
     *  boolean equalsIgnoreCase(String str);忽略大小写比较字符串内容
     * 字符串中是否包含指定字符串?
     * boolean contains(String str);
     * 字符串是否以指定字符串开头,是否以指定字符串结尾。
     * boolean startsWith(String str);
     * boolean endsWith(String str);
     *4、比较功能(比较大于,小于,等于;不是判断字符串是否相同;所以返回值是正数,零,负数)
     * int compareTo(String anotherString)
     *  */
    public class StringDemo2 {
    
        public static void main(String[] args) {
                StringMethod_1();
                StringMethod_2();
                StringMethod_3();
    
        }
    
        private static void StringMethod_3() {
            String s1="abc";
            System.out.println(s1.equals("ABC".toLowerCase()));
            System.out.println(s1.equalsIgnoreCase("ABC"));
            System.out.println(s1.contains("c"));
            String s2="ArrayDemo.java";
            System.out.println(s2.startsWith("Array"));
            System.out.println(s2.endsWith("java"));
            System.out.println(s2.contains("Demo"));
            System.out.println("abc".compareTo("nba"));
        }
    
        private static void StringMethod_2() {
            String s1="张三,李四,王五";
            String[]s2=s1.split(",");
            for (int i = 0; i < s2.length; i++) {
                System.out.println(s2[i]);
        
    }
            char[]s3=s1.toCharArray();
             for (int i = 0; i < s3.length; i++) {
                System.out.println(s3[i]);
            }
            String s4="ab你";
            byte[]s5=s4.getBytes();
            for (int i = 0; i < s5.length; i++) {
                System.out.println(s5[i]);
            }
            String ss="java";
            String sss=ss.replace('q','z');
            System.out.println(ss==sss);//true 原因:字符串一旦声明不会改变,只会去变量池中查找;
            //字符串连接
            System.out.println("abc".concat("nba"));
            System.out.println("abc"+("nba"));
            //static String  valueOf()
            System.out.println(String.valueOf(4)+1);
            System.out.println(""+4+1);
        }
    
        private static void StringMethod_1() {
            String s1="abvdrthsdfdgj"; 
            System.out.println(s1.length());
            System.out.println(s1.indexOf("v"));
            System.out.println(s1.indexOf("d",6));
            System.out.println(s1.lastIndexOf("d",6));
            System.out.println(s1.charAt(2));
            System.out.println(s1.substring(2, 5));
        }
    
    }
    package cn.china;
    /**stringBuffer:就是字符串缓冲区。用于存储数据的容器
     * 特点:
     * 1、长度是可变的。
     * 2、可以存储不同数据类型
     * 3、最终要转成字符串使用
     * 4、可以对字符串可以修改
     * 既然是一个容器,应该具备什么功能?
     * 1、添加: StringBuffer  append(data);
     *        StringBffer   insert(index,data);
     * 
     * 2、删除:StringBuffer  delete(start,end);包含头不包含尾。
     *        StringBuffrt deleteCharAt(int index);删除指定位置元素
     * 3、查找:char  charAt(index);
     *        int  indexOf(String);
     *        int  lastIndexOf(String);
     * 4、修改: StringBuffer replace(start,end,String);
     *        void setCharAt(index,char);
     *        
     *StringBuffer与StringBuilder区别?
     *  jdk1.5以后出现功能与StringBuffer一模一样的对象,就是StringBuilder;
     *  但是StringBuffer线程同步,通常用于多线程;
     *  StringBuilder线程不同步  ,因此在单线程使用StringBuilder速度更快;   
     *  
     *  JDK升级目的:
     *  1、简化书写;
     *  2、提高效率;
     *  3、增加安全性;
     * */
    public class StringBufferDemo1 {
    
        public static void main(String[] args) {
            //缓冲区创建
            StringBuffer s1=new StringBuffer();
            //增加
            StringBuffer s2=s1.append(4);
            s2.append(true);
            s2.append("NBA").append('c');
            s2.insert(1, 0.1);
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s1==s2);
            //删除
            s2.delete(1, 2);
            s2.deleteCharAt(3);
            s2.delete(0, s2.length());//清空缓冲区
            //s2=new StringBuffer();
            //查找
            s2.indexOf("iii");
            //设置长度
            s2.setLength(0);
            //字符串反转
            s2.reverse();
    
        }
    
    }
  • 相关阅读:
    c中uint32转为string
    linux中查找某类文件中的特定字符串
    VMWare Workstation 无法连接外部设备
    Evernote(印象笔记)无法登录的问题
    Python request 在linux上持续并发发送HTTP请求遇到 Failed to establish a new connection: [Errno 11] Resource temporarily unavailable
    设计模式
    jquery-ui sortable 在拖动换位置时改变元素的大小导致占位与实际不一致
    Appium IOS 使用多模拟器并发执行测试
    Fiddler脚本修改Response数据
    android style和attr的用法
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/6645632.html
Copyright © 2011-2022 走看看