zoukankan      html  css  js  c++  java
  • java 操作字符串String和StringBuffer


    java提供两个类来操作字符串:String和StringBuffer。
    如果处理一些小的文本建议使用String类;处理大型文本使用StringBuffer类。
    声明字符串变量:String name;
    初始化字符串变量:name = "lixiansheng";

    String类:
      String类的内容和长度不可变,如果对字符串进行操作则会生成一个新的实例,不会改变对象实例本身。系统为String类对象分配内存时,是按照当前对象所包含的实际字符数分配的。
    StringBuffer类:
      StringBuffer类处理可变字符串,如果要修改一个StringBuffer类的字符串,不需要再创建新的字符串对象,而是直接操作原有字符串。系统为StringBuffer对象分配内存时,除了当前字符所占空间外,还提供一个16字符大小的缓冲区。

    String类处理字符串
      1.字符串类的默认构造器:
        “String()”是最简单的构造器,也是系统默认的构造器,是不带参数的。
        String name = new String(); //创建一个空的字符串

      2.字节参数的构造器
        String(byte[] byte)将字节数组中的元素作为字符串对象。
        byte[] byte = {97, 98, 99};
        String str = new String(byte);
        输出结果为: abc

       3.获取指定字节数的构造器
        String(byte[], int begin, int length) 含义为:将字节数组中从“begin”指定位置开始到“length”长度结束,获取其中间的元素作为字符串对象。 注意数组是从0开始的。
        byte[] byte = {97,98,99,100,101,102};
        String str = new String(byte, 3, 2);
        输出结果为: de


       4.将字节型数据转换为字符集输出的构造器
        String(byte[] byte, int begin, int length, String chatsName) 含义为:获取字节数组中从第“begin”开始到“length”长度结束字符,形成字符串对象,然后按照“charname”字符集输出。
        字符集一般有:“us-ascii”, "iso-8859-1","utf-8","utf-16be","utf-16le","utf-16"等样式。
        byte[] byte = {97,98,99,100,101,102};
        String str = new String(byte, 3, 2, "utf-8");
        输出结果为: de

      5.字符数组构造器
        String(char[], int begin, int length) 含义为:将字节数组中从“begin”指定位置开始到“length”长度结束,获取其中间的字符连城字符串。 注意数组是从0开始的。
        char[] char = {"w", "e", "l", "c", "o", "m", "e"};
        String str = new String(char, 3, 4); //从3开始,截取4位
        输出结果为: come

    String类字符串处理方法
      主要讲:字符串链接 提取子串 从字符串中提取一个指定的字符 字符串长度 检查字符串是否相等 查找特定字符串 从基本类型转换成字符串等
      1.字符串链接

        有两种方法链接字符串:一是使用“+”,另一种是使用方法函数concat(String str).

          

    String str1 = "I am";
            String str2 = "a tester!";
            //使用"+"
            System.out.println(str1 + " " + str2);
            //使用concat
            System.out.println(str1.concat(" ").concat(str2));
    
    输出结果:
    I am a tester!
    I am a tester!

      

      2.提取子字符串
        1)“substring(int index)” 是提取从index指定的位置开始,一直到字符串最后。
        2)“substring(int begin, int end)" 是提取从begin到end位置的字符串。

    public static void main(String[] args) {
            String str1 = "I am a tester!";
            //substring(int index)
            System.out.println(str1.substring(7));
            //substring(int begin, int end)
            System.out.println(str1.substring(5,12));
        }
    
    输出结果:
    tester!
    a teste

      3.从字符串中提取一个指定字符

        charAt(int index),这个方法返回的是一个字符。

    public static void main(String[] args) {
            String str1 = "I am a tester!";
            //获取位置为3的字符
            System.out.println(str1.charAt(3));
        }
    
    输出结果:
    m

      4.获取字符串长度
        str.length().返回字符串长度,类型为int。注意字符串长度length()是方法,数组长度length是属性。

    public static void main(String[] args) {
            String str1 = "I am a tester!";
            //获取长度
            System.out.println(str1.length());
        }
    
    输出结果:
    14  

      5.检查字符串是否相等
        str.equals(str1) 返回true或者false
        str.equalsIgnoreCase(str) 这个方法忽略字符串大小写

    public static void main(String[] args) {
            String str1 = "tester";
            String str2 = "tester";
            String str3 = "Tester";
    
            System.out.println(str1.equals(str2));
            System.out.println(str1.equals(str3));
            System.out.println(str1.equalsIgnoreCase(str3));
        }
    
    输出结果:
    true
    false
    true

      6.查找特定字符
        1)indexOf(子串内容) 子串如果存在str内,则返回首位子串的位置,如子串“sdf”则返回“s”位置;如子串不存在str内,返回负数。
        2)startsWith(子串内容) 测试字符串是否是子串开头。
        3)endsWith(子串内容)测试字符串是否是子串结尾。

    public static void main(String[] args) {
            String str1 = "tester";
    
            System.out.println(str1.indexOf("es"));
            System.out.println(str1.indexOf("esd"));
            System.out.println(str1.startsWith("te"));
            System.out.println(str1.startsWith("a"));
            System.out.println(str1.endsWith("er"));
            System.out.println(str1.endsWith("A"));
        }
    输出结果:
    1
    -1
    true
    false
    true
    false

      7.从基本类型转换成字符串
        valueOf() 将基本类型数据转换成相应的字符串

        int str2= 123;
        String str3 = String.valueOf(str2);


    StringBuffer缓存字符串处理类
      1.默认构造器

    public static void main(String[] args) {
            StringBuffer str = new StringBuffer();
            System.out.println(str.length());  //输出字符串长度
            System.out.println(str.capacity()); //输出字符串容量
        }
    输出结果:
    0
    16

      2.设定容量大小的构造器
        StringBuffer str = new StringBuffer(int x);
        扩容:str.ensureCapacity(int x);

    public static void main(String[] args) {
            StringBuffer str = new StringBuffer(10);
            System.out.println(str.length());  //输出字符串长度
            System.out.println(str.capacity()); //输出字符串容量
            str.ensureCapacity(40);
            System.out.println(str.length());
            System.out.println(str.capacity());
        }
    输出结果:
    0
    10
    0
    40

      3.处理缓存字符串
        1)初始化字符串

    public static void main(String[] args) {
            StringBuffer str = new StringBuffer("老李是名优秀的测试");
            str.append("工程师"); //添加到末尾
            System.out.println(str);
            str.setLength(2);//设置字符串长度
            System.out.println(str);
        }
    输出结果:
    老李是名优秀的测试工程师
    老李

        2)取字符串的单个字符

    str.charAt(int index)
    StringBuffer str = new StringBuffer("老李是名优秀的测试");
            System.out.println(str.charAt(3));
    输出结果:
    名

        3)单个字符赋值

    str.setCharAt(int index,'字符');
     public static void main(String[] args) {
            StringBuffer str = new StringBuffer("老李是名优秀的测试");
            str.setCharAt(0,'小');
            System.out.println(str);
        }
    输出结果:
    小李是名优秀的测试

        4)指定位置插入字符串

    str.insert(int index, str)
    public static void main(String[] args) {
            StringBuffer str = new StringBuffer("老李是名优秀的测试");
            str.insert(9,"工程师");
            System.out.println(str);
        }
    输出结果:
    老李是名优秀的测试工程师

        5)返回字符串子串

    str.substring(int index)
    public static void main(String[] args) {
            StringBuffer str = new StringBuffer("老李是名优秀的测试");
            System.out.println(str.substring(7));
        }
    输出结果:
    测试

        6)倒置字符串内容

    str.reverse()
    public static void main(String[] args) {
            StringBuffer str = new StringBuffer("老李是名优秀的测试");
            System.out.println(str.reverse());
        }
    输出结果:
    试测的秀优名是李老

  • 相关阅读:
    Java 如何有效地避免OOM:善于利用软引用和弱引用
    LRU缓存实现(Java)
    Java实现LRU(最近最少使用)缓存
    HashSet,TreeSet和LinkedHashSet的区别
    IIS-详解IIS中URL重写工具的规则条件(Rule conditions)
    IIS-代理
    IIS-新建网站
    IIS-反向代理配置&&插件安装
    IIS-C#项目环境搭建
    IIS-Windows10如何安装
  • 原文地址:https://www.cnblogs.com/lixianshengfitting/p/13896184.html
Copyright © 2011-2022 走看看