String类的常用方法

/**
 * 关于String类的常用方法
 *     1.    char  charAt(int index),返回指定下标处的字符  返回值char类型
 *
 *     2.    int   compareTo(String anotherString);按照字典顺序比较两个字符串大小,
 *                      返回结果是int,0表示两个字符串相等
 *                                    负数表示前一个比后一个小
 *                                    正数表示前一个比后一个大
 *                      与equals()方法相比,返回值不同,equals()只能比较出来是否相等,而compareTo()能比较出大小.
 *
 *     3.    boolean   contains(CharSequence s); 判断当前字符串是否包含字符串s  返回值布尔类型
 *
 *     4.    boolean   endsWith(String suffix);判断当前字符串是否以字符串suffix结尾  返回值布尔类型
 *
 *     5.    boolean   equals(Object anObject);判断两个字符串是否相等,  返回值是布尔类型
 *
 *     6.    boolean   equalsIgnoreCase(String anotherString);判断两个字符串是否相等,同时忽略大小写  返回值布尔类型
 *
 *     7.    byte[]        getBytes();将字符串转换成字节数组
 *
 *     8.    int       indexOf(String str);判断字符串str在当前字符串中第一次出现的位置的下标(索引)
 *
 *     9.    boolean   isEmpty();判断某个字符串是否为空
 *
 *     10.   int       length();返回字符串的长度。  这里的length()是方法,数组中的length是属性
 *
 *     11.   int       lastIndexOf(String str); 返回字符串str在当前字符串中最后一次出现位置的索引(下标)
 *
 *     12.   String        replace(CharSequence target, CharSequence replacement);字符串替换
 *
 *     13.   String[]  split(String regex);拆分字符串
 *
 *     14.   boolean   startsWith(String prefix);判断前字符串是否以字符串prefix开始
 *
 *     15.   String        substring(int beginIndex);截取字符串,beginIndex起始下标(包括)
 *
 *     16.   String        substring(int beginIndex, int endIndex);截取字符串,[beginIndex起始下标,endIndex结束下标)
 *
 *     17.   char[]        toCharArray();将字符串转换成char数组
 *
 *     18.   String        toLowerCase();将字符串中的字母转换成小写字母
 *
 *     19.   String        toUpperCase();将字符串中的字母转换成大写字母
 *
 *     20.   String        trim();去除字符串前后空白
 *
 *     21.   static String valueOf(Object obj);把非字符串转换成字符串,是String类中唯一的静态方法
 */
class StringTest{
    public static void main(String[] args) {
//        返回指定下标处的字符
        char c = "我是中国人".charAt(2);
        System.out.println(c);//中
        System.out.println("------------------------------");

//        比较两个字符串的大小,0表示相等,正数表示前一个字符串大于后一个字符串,负数表示前一个字符串小于后一个字符串
        int i = "abc".compareTo("abc");
        System.out.println(i);//0
        int i1 = "abc".compareTo("abd");
        System.out.println(i1);//-1
        int i2 = "abc".compareTo("abe");
        System.out.println(i2);//-2
        int i3 = "abe".compareTo("abc");
        System.out.println(i3);//2
        System.out.println("xyz".compareTo("zyx"));//-2
        System.out.println("+++++++++++++++++++++++++++++++++++");

//        判断前一个字符串是否包含后一个字符串
        System.out.println("xyz".contains("y"));//true
        System.out.println("xyz".contains("zy"));//false
        System.out.println("===================================");

//        判断前一个字符串是否以后一个字符串结尾
        System.out.println("abcdf".endsWith("df"));//true
        System.out.println("abcdf".endsWith("de"));//false
        System.out.println("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-");

//        比较两个字符串是否相等,不能不接大小
        System.out.println("abc".equals("aBd"));//false
        System.out.println("abc".equals("abc"));//true
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

//        判断两个字符串是否相等,同时忽略大小写
        System.out.println("abc".equalsIgnoreCase("ABD"));//false
        System.out.println("abc".equalsIgnoreCase("aBc"));//true
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

//        把字符串转换成byte数组
        byte[] bytes = "abcdefg".getBytes();
//        遍历该数组
        for (int j = 0; j < bytes.length ; j++) {
            System.out.print(bytes[j] + " ");//97 98 99 100 101 102 103
        }
        System.out.println();
        System.out.println("***********************************");

//        判断后一个字符串在前一个字符串中第一次出现的位置的下标(索引)
        System.out.println("abcdefghijklmnopq".indexOf("gh"));//6

//        判断字符串是否为空
        String s = "";
        System.out.println(s.isEmpty());//true
        String str = " ";
        System.out.println(str.isEmpty());//false
        String ss = "asd";
        System.out.println(ss.isEmpty());//false
//        String sss = null;
//        System.out.println(sss.isEmpty());//这里会出现空指针异常,null和空字符串不是一个概念

//        判断字符串的长度(注意这里的length是方法,不是属性)
        System.out.println("xyz".length());//3

//        判断后一个字符串在前一个字符串中最后出现位置的下标
        System.out.println("asdfgawsecbmljasfh".lastIndexOf("a"));//14

//        字符串的替换(后一个替换前一个)
        String newString = "http://www.baidu.com".replace("http:","https:");
        System.out.println(newString);//https://www.baidu.com
//        把字符串中的"="换成":"
        String newString1 = "姓名=张三&性别=男&年龄=20".replace("=",":");
        System.out.println(newString1);//姓名:张三&性别:男&年龄:20

//        以某个字符为标准拆分字符串,变成字符串数组
        String[] ymd = "2020-12-11".split("-");
        for (int j = 0; j < ymd.length; j++) {
            System.out.print(ymd[j] + " ");     //2020 12 11
        }
        System.out.println();
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        String[] strings = "姓名=张三&性别=男&年龄=20".split("&");
        for (int j = 0; j < strings.length; j++) {
            System.out.print(strings[j] + " ");     //姓名=张三 性别=男 年龄=20
        }
        System.out.println();
        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

//        判断前一个字符串是否以后一个字符串开始
        System.out.println("http://www.baidu.com".startsWith("http"));//true
        System.out.println("http://www.baidu.com".startsWith("https"));//false

//        从某个下标位置开始截取字符串[开始下标位置字符串,结束下标位置字符串),前闭后开
        System.out.println("http://www.baidu.com".substring(7));//www.baidu.com
        System.out.println("http://www.baidu.com".substring(7,10));//www
        System.out.println("/");

//        将字符串转换成char数组
        char[] chars = "我是中国人".toCharArray();
        for (int j = 0; j < chars.length; j++) {
            System.out.print(chars[j] + " ");
        }
        System.out.println();

//        将字符串中的字母全部变成小写
        System.out.println("ABCdeFGHIZK".toLowerCase());//abcdefghizk

//        将字符串中的字母全部变成大写
        System.out.println("abcghkimhg".toUpperCase());//ABCGHKIMHG

//        去除字符串前后空白(注意该方法不能去除中间部位的空格)
        System.out.println("   zhang    san     ".trim());//zhang    san

//        将非字符串转换成字符串
        System.out.println(String.valueOf(100));//100
        System.out.println(String.valueOf(3.14));//3.14
        System.out.println(String.valueOf(false));//false
        System.out.println(String.valueOf(new User()));//我是中国人!
    }
}

class User{
//    重写toString()方法

    @Override
    public String toString() {
        return "我是中国人!";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值