String常用API

发布时间 2023-10-31 08:48:00作者: keenyy
 
方法名  说明
public int length():
获取字符串当中含有的字符个数,返回字符串长度
public char charAt(int index):
获取指定索引位置的单个字符
public String concat(String str):
将当前字符串和参数字符串str连接,返回值新的字符串
public String toUpperCase():
 
返回所有字母大写的新字符串
public String toLowerCase():
返回所有字母小写的新字符串
public String trim():
返回去掉两边空白字符的新字符串
public boolean equals(String str):
如果该字符串等于字符串str,返回true
public boolean equalsIgnoreCase(String str):
 
如果该字符串等于字符串str,返回true.不区分大小写
 
public int compareTo(String str):
 
返回一个大于0、等于0、小于0的整数,表明一个字符串是否大于、等于或者小于str
 
public int compareToIgnoreCase(String str):
 
返回一个大于0、等于0、小于0的整数,表明一个字符串是否大于、等于或者小于str。不区分大小写
 
public boolean startsWith(String prefix):
 
返回字符串是否以前缀prefix开头
 
public boolean endsWith(String suffix):
 
返回字符串是否以后缀suffix结束
 
public boolean contains(String str):
 
检查字符串中是否包含子串str
public String substring(int begin):
截取字符串,从特定位置begin的字符开始到字符串结尾。
public String substring(int begin,int end):
截取字符串,从特定位置begin的字符开始到end-1的字符。(长度为end - begin)
public int indexOf(String str):
查找参数字符串在本字符串当中首次出现str的索引位置,如果没有返回-1值。
public int indexOf(String str,int fromIndex):
查找参数字符串在本字符串当中fromIndex之后首次出现str的索引位置,如果没有返回-1值。
public int lastIndexOf(String str):
查找参数字符串在本字符串当中最后一个出现str的索引位置,如果没有返回-1值。
public int lastIndexOf(String str,int fromIndex):
查找参数字符串在本字符串当中fromIndex之前出现最后一个str的索引位置,如果没有返回-1值。

 

 

package day3;

import java.util.Scanner;

public class spring{

    public static void main(String[] args) {
        String str1 = "Apple";
        String str2 = "banana";
        System.out.println(str1.compareTo(str2));//返回小于0的整数,str1比str2小
        System.out.println(str2.compareTo(str1));//返回大于0的整数,str2比str1大
        System.out.println(str2.compareTo("banana"));//返回0, 相等

        
        System.out.println(str2.compareTo("Banana"));
        System.out.println(str2.compareToIgnoreCase("Banana"));//不区分大小写返回0
        
        String token = "Bear-eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjEzNTAwMDAxMTExIiwiaWQiOjJ9.gjbMpA1t6eor6iC66II21wkfayXiMXL_8IPHM4Yc728";
        System.out.println(token.startsWith("Bear-")); //token字符串是否以特定前缀Bear-开头
        System.out.println(token.endsWith("token")); //false,是否以token结尾
        
        System.out.println(token.contains("eyJwaG"));//true,字符串中是否有该字符
        
        
        String email = "zhangrui@uestc.edu.cn";
        System.out.println(email.indexOf("@"));
        System.out.println(email.indexOf("."));//返回第一次出现.的索引位置
        System.out.println(email.indexOf("peppa"));//在email字符串中找不到peppa子串,返回-1
        
        System.out.println(email.lastIndexOf("."));//最后一次出现.的索引位置
        
        //字符串截取
        //substring(index):从指定的索引位置截取到字符串的结尾
        if(email.indexOf("@") != -1) {
            System.out.println("邮箱域名:" + email.substring(email.indexOf("@") + 1));
            //substring(begin,end):从begin索引位置开始截取,截取到end索引位置(不包含end,长度为:end-being)
            System.out.println("用户名:" + email.substring(0, email.indexOf("@")));
            
            int number = 2000;
            String srt = number + "";
            System.out.println(srt.length());//输出字符串长度
        }
        
        
    }

}