String类中常用的方法——Java常用类

String类中常用的方法

	//1.(掌握)public char charAt(int index)
    char c = "歪比巴卜".charAt(0);
    System.out.println(c);  //歪

    //2.(了解)public int compareTo(String anotherString)
    int result1 = "abc".compareTo("abc");
    System.out.println(result1);    //0(等于0) 前后一致
    int result2 = "abcd".compareTo("abce");
    System.out.println(result2);    //-1(小于0) 前小后大
    int result3 = "abce".compareTo("abcd");
    System.out.println(result3);    //1(大于0) 前大后小
    //如果第一个字母就能比出大小,则后面的字母不再比较
    System.out.println("abc".compareTo("bcd")); //-1

    //3.(掌握)public boolean contains(CharSequence s)
    //判断前面的字符串中是否包含后面的子字符串
    System.out.println("HelloWorld.java".contains(".java"));  //true
    System.out.println("http://www.baidu.com".contains("https://"));  //false

    //4.(掌握)public boolean endsWith(String suffix)
    //判断当前字符串是否以某个字符串结尾
    System.out.println("test.txt".endsWith(".java"));   //false
    System.out.println("test.txt".endsWith(".txt"));    //true
    System.out.println("kasdfhuwss".endsWith("ss"));    //true

    //5.(掌握)public boolean startsWith(String prefix)
    //判断当前字符串是否以某个字符串开始
    System.out.println("http://www.baidu.com".startsWith("http"));   //true
    System.out.println("http://www.baidu.com".startsWith("https"));  //false

    //6.(掌握)public boolean equals(Object anObject)
    //比较字符串是否相等要用equals方法
    System.out.println("abcd".equals("abcd"));  //true

    //7.(掌握)public boolean equalsIgnoreCase(String anotherString)
    //忽略大小写,判断两个字符串是否相等
    System.out.println("abC".equalsIgnoreCase("ABC"));  //true

    //8.(掌握)public int indexOf(String str)
    //判断某个子字符串,在当前字符串中第一次出现处的索引(下标)
    //如果不存在,则返回-1。
    System.out.println("阿巴阿巴 java c++ python php java c++ python php".indexOf("java")); //5

    //9.(掌握)public int lastIndexOf(String str)
    //判断某个子字符串,在当前字符串中最后一次出现处的索引(下标)
    System.out.println("阿巴阿巴 java c++ python php java c++ python php".lastIndexOf("java")); //25

    //10.(掌握)public boolean isEmpty()
    //判断字符串是否为空字符串
    System.out.println("".isEmpty());   //true

    //11.(掌握)public int length()
    //判断数组长度是length属性,判断字符串长度是length()方法
    System.out.println("qwe".length()); //3
    System.out.println("".length());    //0

    //12.(掌握)public String replace(CharSequence target, CharSequence replacement)
    //替换
    //String的父接口就是:CharSequence
    String str = "http://www.baidu.com".replace("http://", "https://");
    System.out.println(str);    //https://www.baidu.com
    String newStr = "name=zhangsan&password=123&age=20".replace("=", ":");
    System.out.println(newStr); //name:zhangsan&password:123&age:20

    //13.(掌握)public String[] split(String regex)
    //拆分字符串
    String[] ymd = "1980-10-11".split("-");    //将"1980-10-11"以"-"为分隔符进行拆分
    for(String i : ymd) {
        System.out.print(i + " ");
    }
    System.out.println();
    String param = "name=zhangsan&password=123&age=20";
    String[] params = param.split("&");
    for(String i : params) {
        System.out.print(i + " ");
    }
    System.out.println();

    //14.(掌握)public String substring(int beginIndex)
    //截取字符串(参数是起始下标)
    System.out.println("http://www.baidu.com".substring(7));    //www.baidu.com

    //15.(掌握)public String substring(int beginIndex, int endIndex)
    //截取字符串( 参数是起始下标(包括),结束下标(不包括) )
    System.out.println("http://www.baidu.com".substring(11, 16));   //baidu

    //16.(掌握)public byte[] getBytes()
    //将字符串对象转换成byte数组
    byte[] bytes = "abcdef".getBytes();
    for (int i : bytes) {
        System.out.print(i + " ");
    }
    System.out.println();

    //17.(掌握)public char[] toCharArray()
    //将字符串转换成char数组
    char[] chars = "歪比巴布".toCharArray();
    for (char newChar : chars) {
        System.out.print(newChar + " ");
    }
    System.out.println();
     
    //18.(掌握)public String toLowerCase()
    //转换成小写
    System.out.println("WbwB,wbBb".toLowerCase());  //wbwb,wbbb

    //19.(掌握)public String toUpperCase()
    //转换成大写
    System.out.println("WbwB,wbBb".toLowerCase());  //wbwb,wbbb

    //20.(掌握)public String trim()
    //去除字符串前后空白(中间的空白不会去除)
    System.out.println("    hello,   world   ".trim()); //hello,   world

    //21.(掌握)public static String valueOf(各种类型的参数)
    //String中唯一的静态方法
    //将“非字符串”转换成“字符串”
    //当参数是一个对象时,会调用该对象的toString()方法
    String s1 = String.valueOf(true);
    System.out.println(s1);
    String s2 = String.valueOf(100);
    System.out.println(s2);
    String s3 = String.valueOf(3.14);
    System.out.println(s3);

    Object anObj = new Object();
    //通过源码可以看出,为什么输出一个引用的时候,会调用toString()方法!!!
    //本质上System.out.println()这个方法在输出任何数据的时候都会先转换成String类型,再输出。
    System.out.println(anObj);
    //System.out.println()调用了valueOf(),valueOf()又调用了toString()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值