• java基础-stringAPI


    一 前言

    这篇文章是针对初学者学习字符串使用的文章,在之前的基础系列文章中有使用过字符串,但是没有详细的讲解如何使用,这篇文章将会大方面将解String的API操作;

    二 构造方法

    String 的构造方法比较多,知识追寻者会挑选几个重点构造方法进行说明;String是不可变的字符串,每次使用StringAPI 都会生成新的字符串对象;想要详细了解底层内容,知识追寻者会在文末给出链接,初学者跳过;

    2.1 String()

    String() 构造方法是创建一个空的字符串对象;

        public static void main(String[] args) {
            String s = new String();
            // 输出为空白行
            System.out.println(s);
            // 重新赋值
            s = "知识追寻者";
            // 知识追寻者
            System.out.println(s);
        }
    

    2.2 String(String original)

    从参数给定的字符串,复制一份并生成新的字符串;

      public static void main(String[] args) {
            String s = new String("知识追寻者");
            // 知识追寻者
            System.out.println(s);
        }
    

    2.3 String(byte[] bytes)

    使用平台默认的字符集进行解码,将一个字节数组转为字符串;

       public static void main(String[] args) {
           // 创建
           String str = new String("知识追寻者");
           // 获得字节数组
           byte[] bytes = str.getBytes();
           // 解码生成字符串
           String s = new String(bytes);
           // 知识追寻者
           System.out.println(s);
       }
    

    2.4 String(byte[] bytes, String charsetName)

    使用指定的解码方式,并生成一个字符串;
    String(byte[] bytes, Charset charset) 方法同这个方法功能差不多,在指定字符集时使用的是Charset对象进行指定;

        public static void main(String[] args) throws UnsupportedEncodingException {
            // 创建
            String str = new String("知识追寻者");
            // 获得字节数组
            byte[] bytes = str.getBytes();
            // 解码生成字符串
            String s = new String(bytes,"UTF-8");
            // 知识追寻者
            System.out.println(s);
        }
    

    2.5 String(char[] value)

    将字符数组生成新的字符串;

        public static void main(String[] args) {
            // 创建字符数组
            char[] chars = {'3', '4', '5'};
            String s = new String(chars);
            // 345
            System.out.println(s);
        }
    

    2.6 String(StringBuilder builder)

    StringBuilder 是可变的字符串,但线程不安全,使用String(StringBuilder builder) 会将可变字符串转为String不可变字符串;

        public static void main(String[] args) {
            // 创建可变的字符串
            StringBuilder builder = new StringBuilder("知识追寻者");
            String s = new String(builder);
            // 知识追寻者
            System.out.println(s);
        }
    

    2.7 String(StringBuffer buffer)

    StringBuilder 是可变的字符串,但线程安全,使用String(StringBuilder builder) 会将可变字符串转为String不可变字符串;

        public static void main(String[] args) {
            StringBuffer buffer  = new StringBuffer("知识追寻者");
            String s = new String(buffer);
            // 知识追寻者
            System.out.println(s);
        }
    

    三 字符串拼接

    字符串拼接意指将多个字符串进行拼接成新的字符串;

    3.1 使用 + 拼接

    使用 + 进行拼接会将其他基本数据类型转为新的字符串;下面示例中使用 字符串变量和整型变量拼接生成新的字符串;其余类型类似;

        public static void main(String[] args) {
            String str1 = "知识追寻者";
            String str2 = "很爱学习";
            // 字符串拼接字符串
            String s1 = str1 + str2;
            // 字符串拼接其他数据类型
            int i = 666;
            String s2 = str1 + i;
            // 知识追寻者很爱学习
            System.out.println(s1);
            // 知识追寻者666
            System.out.println(s2);
    
        }
    

    3.2 String concat(String str)

    使用 String concat(String str) 能将多个字符串拼接成一个字符串;

        public static void main(String[] args) {
            String str1 = "知识追寻者";
            String str2 = "很爱学习";
            String s = str1.concat(str2);
            // 知识追寻者很爱学习
            System.out.println(s);
        }
    

    四 字符串查询

    字符串的位置默认是从0开始计算;查询字符串的返回值都是int类型,返回的是子串的位置; lastIndexOf 系列API 与indexOf 系列API类似,区别是indexOf 查找的是出现第一个的字符串字串,lastIndexOf 是查找最后一个字符字串的位置; 正确返回会出现字串在字符串中的位置,错误则一般报错返回-1;

    4.1 indexOf(int ch)

    顺序查询第一个字符出现的位置;

       public static void main(String[] args) {
            String str = "123123123";
            int indexOf = str.indexOf('2');
            // 1
            System.out.println(indexOf);
        }
    

    4.2 indexOf(String str)

    顺序查询第一个子串出现的位置;

        public static void main(String[] args) {
            String str = "123123123";
            int indexOf = str.indexOf("123");
            // 0
            System.out.println(indexOf);
        }
    

    4.3 indexOf(int ch, int fromIndex)

    从指定的位置,顺序查询第一个出现的字符;第一组的123 的位置 是 012 ;第二组123 的位置是 345 ; 第三组123的位置是 678 ; 显然从 2 位置开始查找字符’2‘, 就是从第二组123开始查询字符'2',第二组2字符的位置是4;

        public static void main(String[] args) {
            String str = "123123123";
            int indexOf = str.indexOf('2',2);
            // 4
            System.out.println(indexOf);
        }
    

    4.4 indexOf(String str, int fromIndex)

    从指定的位置,顺序查询第一个出现子串的位置;从字符串位置1开始查找字串123的位置也就是第二组123的起始位置,即3;

         public static void main(String[] args) {
             String str = "123123123";
             int indexOf = str.indexOf("123",1);
             // 3
             System.out.println(indexOf);
         }
    

    五 字符串包含

    contains(CharSequence s) 判定字符串中是否包含字串序列,如果包含返回true,否则返回false;

        public static void main(String[] args) {
            String str = "知识追寻者";
            boolean p1 = str.contains("追寻者");
            // true
            System.out.println(p1);
            boolean p2 = str.contains("33");
            // false
            System.out.println(p2);
        }
    

    六 字符串比较

    6.1 compareTo(String anotherString)

    两个字符串比较大小是根据每个字符Unicode 值在字符串中进行比较,当 前者 大于 后者返回 正数,前者等于后者 返回0;前者小于 后者返回负数;compareToIgnoreCase(String str) 与 compareTo(String anotherString) 区别就是会忽略字符串中字符的大小写,其他都一样;

        public static void main(String[] args) {
            String str1 = "a";
            String str2 = "b";
            String str3 = "A";
            int compare1 = str1.compareTo(str2);
            // -1
            System.out.println(compare1);
            int compare2 = str1.compareTo(str3);
            // 32
            System.out.println(compare2);
            int compare3 = str2.compareTo(str3);
            // 33
            System.out.println(compare3);
            int compare4 = str2.compareTo(str2);
            // 0
            System.out.println(compare4);
        }
    

    6.2 equals(Object anObject)

    equals(Object anObject) 是比较字符串内容是否相等;equalsIgnoreCase(String anotherString) 与 equals(Object anObject) 却别是在比较的时候会忽略大小写,其余都相同;

        public static void main(String[] args) {
            String str1 = "学知识";
            String str2 = "学知识";
            String str3 = "好好好";
            boolean equals1 = str1.equals(str2);
            // true
            System.out.println(equals1);
            boolean equals2 = str1.equals(str3);
            // false
            System.out.println(equals2);
        }
    

    七 字符串匹配

    字符串匹配是根据正则表达式进行匹配,如果完全匹配返回true,否则false;初学者跳过,以后可以深入学习;

        public static void main(String[] args) {
            String str = "123456789";
            boolean matches = str.matches("[0-9]*");
            // true
            System.out.println(matches);
        }
    

    八 字符串截取

    8.1 substring(int beginIndex)

    substring(int beginIndex) 从指定的位置开始截取字串至字符串末尾;下面示例中的位置是2,从2的位置(包括2)开始截取字串为 追寻者 ;

        public static void main(String[] args) {
            String str = "知识追寻者";
            String substring = str.substring(2);
            // 追寻者
            System.out.println(substring);
        }
    

    8.2substring(int beginIndex, int endIndex)

    substring(int beginIndex, int endIndex), 截取 beginIndex 位置 (包括)到 endIndex 位置(不包括)的字串;
    也有喜欢是说成截取从 beginIndex 至 endIndex -1 位置的字串;

    
        public static void main(String[] args) {
            String str = "知识追寻者";
            String substring = str.substring(2, 3);
            // 追
            System.out.println(substring);
        }
    

    九 字符串代表系列

    字符串代表系列就是String代表的基本数据类型;也就是说基本数据类型都是可以通过valueOf 方法转为字符串类型;字符串类型是万能的了;

    方法 代表含义
    valueOf(boolean b) 布尔类型代表的字符串
    valueOf(char c) 字符类型代表的字符串
    valueOf(char[] data) 字符数组类型代表的字符串
    valueOf(char[] data, int offset, int count) 子字符数组代表的字符串
    valueOf(double d) 双精度型代表的字符串
    valueOf(float f) 单精度类型代表的字符串
    valueOf(int i) 整型代表的字符串
    valueOf(long l) 长整型代表的字符串
    valueOf(Object obj) 对象代表的字符串

    使用示例:

        public static void main(String[] args) {
            double d = 123d;
            String s = String.valueOf(d);
            // 123.0
            System.out.println(s);
        }
    

    十 参考文献

    还有其他的String API 读者可以参照官方文档自行研究,大部分常用的系列的API都已经讲到;

    java8doc: https://docs.oracle.com/javase/8/docs/api/
    Stringbuffer 和 Stringbuild 区别: https://blog.csdn.net/youku1327/article/details/102640856
    String深入研究

  • 相关阅读:
    读取组合单元格
    Spire.XLS:一款Excel处理神器
    linq
    LINQ语句中的.AsEnumerable() 和 .AsQueryable()的区别
    合并单元格
    web sec / ssd / sshd
    linux——cat之查看cpu信息、显示终端、校验内存.............
    MATLAB mcr lib的环境变量书写
    Linux查看库依赖方法
    判断当前所使用python的版本和来源
  • 原文地址:https://www.cnblogs.com/zszxz/p/12122616.html
Copyright © 2020-2023  润新知