首页 > 编程语言 > 详细

Java学习笔记 - String类常用方法

时间:2020-05-04 18:56:19      阅读:40      评论:0      收藏:0      [点我收藏+]

java.lang.String
String表示字符串类型,属于引用数据类型,不属于基本数据类型

构造方法

1.String s = new String("");
2.String s = "";
3.String s = new String(char);
4.String s = new String(char, 起始下标,长度);
5.String s = new String(byte);
6.String s = new String(byte, 起始下标,长度);

示例程序

public class StringTest04 {
    public static void main(String[] args) {
        //a b c
        byte[] bytes = {97,98,99};
        System.out.println("String s = new String(byte)");
        String s2 = new String(bytes);
        System.out.println(s2);

        System.out.println("String s = new String(byte, 起始下标,长度)");
        String s3 = new String(bytes , 1, 2);
        System.out.println(s3);

        char[] chars = {‘开‘ , ‘心‘, ‘啊‘};
        System.out.println("String s = new String(char)");
        String s4 = new String(chars);
        System.out.println(s4);

        System.out.println("String s = new String(byte, 起始下标,长度)");
        String s5 = new String(chars, 0 , 2);
        System.out.println(s5);
    }
}

技术分享图片

常用方法

public class StringTest05 {
    public static void main(String[] args) {
        //1.char charAt(int index)
        char c = "中国人".charAt(1);
        System.out.println(c);  //国

        //2(了解)int compareTo(String anotherString)
        //等价于 strcmp
        int result = "abc".compareTo("abc");
        System.out.println(result);   //0

        int result1 = "abcd".compareTo("abce");
        System.out.println(result1);	  //-1

        int result2 = "abc".compareTo("abc");
        System.out.println(result2);  //1


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


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


        //5.boolean equalsIgnoreCase(String anotherString)
        // 判断两个字符串是否相等,并且同时忽略大小写
        System.out.println("abc".equalsIgnoreCase("Abc")); //true

        //6. byte[] getBytes()
        //将字符串对象转换成字节数组
        byte[] bytes = "abcdef".getBytes();
        for(int i = 0; i < bytes.length;i++)
            System.out.println(bytes[i]);   //97 98 99 100...

        //7.int indexOf(String str)
        //判断某个字符串在当前字符串中第一次出现处的索引(下标)
        System.out.println("oracleJava".indexOf("Java"));   //6


        //8.boolean isEmpty()
        //判断一个字符串是否为空
        String s8 = "";
        System.out.println(s8.isEmpty());  //true

        //9.int length()
        //判断数组长度是length属性,判断字符串长度是length()方法
        System.out.println(s8.length());   //0

        //10. int.lastIndexOf(String str)
        //判断某个字符串在当前字符串中最后一次出现处的索引(下标)
        System.out.println("oracleJavaJava".lastIndexOf("Java"));   //10

        //11.String replace(CharSequence target, CharSequence replacement)
        //String的父接口就是:CharSequence
        String newString = "http://www.baidu.com".replace("http","https");

        //12. String[] split(String regex)
        //拆分字符串
        String[] ymd = "1980-10-11".split("-");  //以"-"分隔符进行拆分
        for(int i = 0; i < ymd.length;i++)
            System.out.println(ymd[i]);    //1980 10 11

        //13.boolean startsWith(String prefix)
        //判断某个字符串是否以某个子字符串开始
        System.out.println("http://www.baidu".startsWith("http://"));  //true


        //14. String substring(int beginIndex)
        //截取字符串,参数是起始下标
        System.out.println("oracleJavaJava".substring(6));  //JavaJava
        //String substring(int beginIndex, int endIndex)
        //截取字符串,左闭右开
        System.out.println("oracleJavaJava".substring(6,10));  //Java

        //15.char[] toCharArray()
        //将字符串转化成char数组
        char[] chars = "HelloWorld".toCharArray();
        for(int i = 0; i < chars.length;i++)
            System.out.println(chars[i]);   // H e l l o ....

        //16. String toLowerCase()
        //转化为小写
        System.out.println("ABcsdf".toLowerCase());    //abcsdf
        //String toUpperCase()                     
        //转化为大写
        System.out.println("ABcsdf".toUpperCase());    //ABCSDF

        //17.String trim()
        //去除字符串前后的空格
        System.out.println("   ABcsdf   ".trim());    //ABcsdf

        //18.String只有一个方法是静态的,不需要new对象
        //valueOf
        //作用:将非字符串转化成字符串
        //当参数是一个对象的时候,会自动调用该对象的toString方法
        String s1 = String.valueOf(3.1415);       //3.1415
        System.out.println(s1);
        System.out.println(new Customer());       //VIP用户

        //19.println的源码:对于引用类型,调用valueOf方法,而valueOf方法会自动调用toString方法
        System.out.println(100);
        System.out.println(true);
        System.out.println(new Customer());
    }
}

class Customer{
    @Override
    public String toString() {
        return "VIP客户";
    }
}

Java学习笔记 - String类常用方法

原文:https://www.cnblogs.com/zy200128/p/12827544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!