Java基础String类常用方法

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

1、(掌握)charAt(int index);

char c = "中国人".charAt(1);//"中国人"是一个字符串Strin对象。只要是对象就能“点.”
System.out.println(c);//国

2、(了解)int compareTo(String anotherString);
符串之间比较大小不能直接使用><,需要使用compareTo方法

int result1 = "abc".compareTo("abc");
System.out.println(result1);//0 前后一致10-10=0

int result2 = "abcd".compareTo("abce");
System.out.println(result2);//-1 前小后大 8-9=-1

int result3 = "abce".compareTo("abcd");
System.out.println(result3);//1 前大后小 9-8=1

//拿着字符串第一个字母和后面的第一个字母比较。能分胜负就不在比较了。
int result4 = "xyz".compareTo("yxz");
System.out.println(result4);//-1

3、(掌握)boolean contains(CharSequence s);
判断前面的字符串中是否包含后面的字符串

System.out.println("HelloWorld.java".contains(".java"));//true
System.out.println("http://www.baidu.com".contains("https://"));//false

4、(掌握)booelan endsWith(String suffix);
判断当前字符串是否以某个字符串结尾

System.out.println("test.txt".endsWith(".java"));//false
System.out.println("test.txt".endsWith(".txt"));//true
System.out.println("fffjsfuinionszcjiss".endsWith("ss"));//true

5、(掌握)boolean equals(Object abObject);
比较两个字符串必须使用equals方法,不能使用“==”
equals方法有没有调用compareTo?老版本可以看一下。JDK13中并没有调用compareTo()方法

System.out.println("abc".equals("abc"));//true

6、(掌握)boolean equalsIgnoreCase(String anotherString);
判断两个字符串是否相等,并且同时忽略大小写

System.out.println("ABC".equalsIgnoreCase("abC"));//true

7、(掌握)byte[] getBytes();
将字符串对象转换成字节数组

byte[] bytes = "abcdef".getBytes();
for(int i = 0;i< bytes.length;i++){
    System.out.println(bytes[i]);
}

8、(掌握)int indexof(String str);
判断某个子字符串在当前字符串中第一次出现处的索引(下标)

System.out.println("orclejavac++.net#phppythonjavaorclec++".indexOf("java"));//6

9、(掌握)boolean isEmpty();
判断某个字符串是否为“空字符串”。底层源代码调用的应该是字符串的length()方法

String s = "";
String s1 = "abc";
//String s2 = null;
System.out.println(s.isEmpty());//true
System.out.println(s1.isEmpty());//false
//System.out.println(s2.isEmpty());//空指针异常

10、(掌握)int length();
面试题:判断数组长度和判断字符串长度不一样
判断数组长度是length属性,判断字符串长度是length()方法

System.out.println("abcdef".length());//6

11、(掌握)int lastIndexof(String str);
判断某个子字符串在当前字符串中最后一次出现的索引

System.out.println("orclejavac++.net#phppythonjavaorclec++".lastIndexOf("java"));//26

12、(掌握)String replace(CharSequence target, CharSequence replacement);
替换
Stringde 父接口就是:CharSequence

String newString = "http://www.baidu.com".replace("http://", "https://");
System.out.println(newString);
//把以下字符串中的”=“替换成”:“
String newString2 = "name=zhangsan&password=123&age=20".replace("=", ":");
System.out.println(newString2);

13、(掌握)String[] split(String regex);
拆分字符串

String[] ymd = "1980-9-19".split("-");
for(int i = 0;i< ymd.length;i++){
    System.out.println(ymd[i]);
}
String[] param = "name=zhangsan&password=123&age=20".split("&");
for(int i = 0;i<ymd.length;i++){
    System.out.println(param[i]);
}

14、(掌握)boolean startswith(String prefix);

System.out.println("http://baidu.com".startsWith("http://"));//true
System.out.println("http://baidu.com".startsWith("https://"));//false

15、(掌握)String substring(int beginIndex);参数是起始下标
截取字符串

System.out.println("http://www.baidu.com".substring(7));//www.baidu.com

16.(掌握)String substring(int beginIndex,int endIndex);
beginIndex起始位置(包括)
endIndex结束位置

System.out.println("http://www.baidu.com".substring(7,10));//www

17、(掌握)char[] toCharArray();
将字符串转换成char数组

char[] chars = "我是中国人".toCharArray();
for(int i = 0;i<chars.length;i++){
   System.out.println(chars[i]);
}

18、(掌握)String toLowerCase();
转换为小写

System.out.println("ABCdeGHcsf".toLowerCase());//abcdeghcsf

19、(掌握)String toUpperCase();
转换为大写

System.out.println("ABCdeGHcsf".toUpperCase());//ABCDEGHCSF

20、(掌握)Sting trim();
去除字符串前后空白

System.out.println("      hello      world".trim());

21、(掌握)String中只有一个方法是静态的,不需要new对象
这个方法叫做valueof
作用:将“非字符串”转换成“字符串”

//String s2 = String.valueOf(100);
        //String s2 = String.valueOf(true);
        String s2 = String.valueOf(3.14);
        //这个静态方法valueof(),参数是一个对象的时候,会自动调用该对象的toString()方法?
        String s3 = String.valueOf(new Customer());
        System.out.println(s2);//3.14
        //System.out.println(s3);//没有重写toString方法之前是对象的内存地址
        System.out.println(s3);//我是一个VIP客户!!!

        Object obj = new Object();
        //通过源码可以看出:为什么输出一个引用的时候,会调用toString()方法!!!
        //本质上System.out.println()这个方法在输出任何数据的时候都是先转换成字符串,在输出
        System.out.println(obj);
        System.out.println(new Customer());
        
    }
}

class Customer{

    //重写toString方法
    @Override
    public String toString() {
        return "我是一个VIP客户!!!";
    }
}

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

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

(0)
小半的头像小半

相关推荐

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