String类的常用方法

导读:本篇文章讲解 String类的常用方法,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

public class DemoString {
    public static void main(String[] args) {
        // 创建字符串对象
        String s1 = "hello";
        String s2 = "hello";
        String s3 = "HELLO";

        // boolean equals(Object obj):比较字符串的内容是否相同
        System.out.println(s1.equals(s2));  // true
        System.out.println(s1.equals(s3));  // false
        System.out.println("-----------分割线------------");

        // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s2));  // true
        System.out.println(s1.equalsIgnoreCase(s3));  // true
        System.out.println("-----------分割线------------");

        /*
         * 面试题:==和equals的区别?
         * 答:2个字符串使用==比较运算符,比较的是地址值,如果使用的是equals方法,
         * 比较的是字符串内容是否相等.
         */

        // int length():获取字符串的长度,其实也就是字符个数
        System.out.println(s1.length());  // 5
        System.out.println("-----------分割线------------");

        // String concat:将指定的字符串连接到该字符串的末尾
        System.out.println(s1.concat(s3));  // helloHELLO
        System.out.println("-----------分割线------------");

        // char charAt:获取指定索引处的字符
        System.out.println(s1.charAt(4));  // o
        System.out.println(s1.charAt(0));  // h
        System.out.println("-----------分割线------------");

        // int indexOf (String str) :获取子字符串第一次出现在该字符串内的索引,没有返回-1
        System.out.println(s1.indexOf("h"));  // 0
        System.out.println(s1.indexOf("dd"));  // -1
        // lastIndexOf:目标字符或字符串在源字符串中最后一次出现的位置下标
        System.out.println(s1.lastIndexOf("l"));  // 3
        System.out.println("-----------分割线------------");

        // String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
        System.out.println(s1.substring(2));  // llo
        System.out.println(s1.substring(3));  // lo
        // String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
        System.out.println(s1.substring(0,2));  // he
        System.out.println(s1.substring(1,4));  // ell
        System.out.println("-----------分割线------------");

        // char[] toCharArray:把字符串转换为字符数组
        char[] chars = s1.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.println(chars[i]);
        }
        System.out.println("-----------分割线------------");

        // byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
        byte[] bytes = s1.getBytes();
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }
        System.out.println("-----------分割线------------");

        // String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。
        System.out.println(s1.replace("he", "xx"));  // xxllo
        System.out.println("-----------分割线------------");

        // String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
        // 有些特殊符号需要用 反斜杠 \ 转义,在Java要用两个反斜杠 \\
        String s = "aa|bb|cc";
        String[] split = s.split("\\|");
        for (String str : split) {
            System.out.println(str); // aa bb cc
        }
        System.out.println("-----------分割线------------");

        // boolean contains(CharSequence s): 判断字符串中是否包含指定字符。
        System.out.println(s.contains("aa"));  // true
        System.out.println(s.contains("gg"));  // false
        System.out.println("-----------分割线------------");

        // static String valueOf(xxx xx):基本类型转换为字符串
        String.valueOf(12.88);

        // String replaceFirst(String regex,String replacement):替换第一个匹配到的字符串
        System.out.println(s1.replaceFirst("l", "p"));  // heplo
        // String replaceAll(String regex,String replacement):替换所有匹配到的字符串
        System.out.println(s1.replaceAll("l", "p"));  // heppo
        System.out.println("-----------分割线------------");

        // String trim():去除字符串两端的空格,中间的空格不变
        String ss = " app le  ";
        System.out.println(ss.trim());  // "app le"
        System.out.println("-----------分割线------------");

        // 字符串中字符的大小写转换
        // String toLowerCase():转换成小写
        System.out.println(s3.toLowerCase());  // hello
        // String toUpperCase():转换成大写
        System.out.println(s1.toUpperCase());  // HELLO
        System.out.println("-----------分割线------------");

        // 对字符串内容按字典顺序进行大小比较,
        // 通过返回的整数值指明当前字符串与参数字符串的大小关系。
        // 若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
        System.out.println(s1.compareTo(s3));  // 32
        System.out.println(s3.compareTo(s1));  // -32
        System.out.println(s1.compareTo(s2));  // 0
        System.out.println(s1.compareToIgnoreCase(s3));  // 0
        System.out.println("-----------分割线------------");

        // 字符串长度是否为0
        String a = "";
        System.out.println(s1.isEmpty());  // false
        System.out.println(a.isEmpty());  // true
        System.out.println("-----------分割线------------");

        // 是否以目标字符串开头
        System.out.println(s1.startsWith("he"));  // true
        // 是否以目标字符串结束
        System.out.println(s1.endsWith("l"));  // false
        System.out.println("-----------分割线------------");

        // 字符串是否匹配正则表达式
        String phone = "13376850345";
        String reg = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        System.out.println(phone.matches(reg));  // true
        System.out.println("-----------分割线------------");

        // 格式化字符串 除了%s外还有各种转换符,自行百度
        System.out.println(String.format("hi, %s", "jack"));  // hi, jack

    }
}

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/3039.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!