String常用方法和举例

String常用方法:


package com.cheng.string;


public class Methord01 {
   public static void main(String[] args) {
       String str = "HelloWorld";
       System.out.println(str.length());//返回字符串长度
       System.out.println(str.charAt(2));//charAt返回指定索引处的字符,此处为1,输出e
       System.out.println(str.isEmpty());//inEmpty判断是否是空字符串,布尔类型。
       String s1 = str.toLowerCase();//toLowerCase将str所有字符全部转换为小写,用s1接收
       String s2 = str.toUpperCase();//toUpperCase将str所有字符全部转换为大写,用s2接收
       System.out.println(str);//HelloWorld
       System.out.println(s1);//helloworld
       System.out.println(s2);//HELLOWORLD
       String str2 = "   hel lwo dsa   ";
       String s3 = str2.trim();//trim返回字符串的副本,忽略前导空白和尾部空白。
       System.out.println(str.equals(str2));//equals判断两字符串内容是否一样,输出false
       System.out.println(str.equalsIgnoreCase(str2));//equalsIgnoreCase
//在忽略大小写时判断两字符串内容是否一样,输出false
       String s4 = str.concat("AAA");//concat 将指定字符串连接到此字符串的结尾,等价于+
       System.out.println(str.compareTo(str2));//compareTo : str和str2比较大小。 输出40
       String s5 = str.substring(2);//substring:取从索引处开始(包括)直到结束的字串。
       String s6 = str.substring(2,5);//substring:取从索引处开始到索引结束,前闭后开的字串。
       System.out.println(s5);//lloWorld
       System.out.println(s6);//llo
  }
}

 


 

 

 

public class Methord02 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        boolean b1 = s1.endsWith("ld");//true 是否以指定字符结尾
        boolean b2 = s1.startsWith("ada");//false 是否以指定字符开头
        boolean b3 = s1.startsWith("ll",2);//从指定索引出开始是否以指定字符开头 true
        System.out.println(b1+" "+b2+" "+b3);
        String s2 = "ll";
        System.out.println(s1.contains(s2));//判断s1串是否包含s2串
        System.out.println(s1.indexOf(s2));//返回s2在s1中第一次出现的位置索引,输出2 若找不到输出-1
        System.out.println(s1.indexOf(s2,1));//返回s2在s1中3索引之后第一次出现的位置 若找不到输出-1
        System.out.println(s1.lastIndexOf(s2));//从后往前找,返回s2在s1中第一次出现的位置索引,
//输出2 若找不到输出-1
        System.out.println(s1.lastIndexOf(s2,5));//在指定索引处从后往前找,
//返回s2在s1中3索引之后第一次出现的位置 若找不到输出-1
    }
}

 


 

package com.cheng.string;
public class Methord03 {
    public static void main(String[] args) {
        String str = "helloworld";
        String str1 = str.replace('h','H');//替换字符
        String str2 = str.replace("hel","HEL");//替换字符序列(串)
        System.out.println(str1+" "+str2);//输出Helloworld HELloworld
        str = "1234567";
        boolean matches = str.matches("\\d+");//matches 匹配
        System.out.println(matches);
        //判断str中是否全是数字,d表示数字,+表示多个
        str =  "0213-2314122";
        matches = str.matches("0213-\\d{7,8}");
        System.out.println(matches);
        //前面判断0213-是否匹配,d{7,8}表示7-8位数字
        str1 = "hello|world|java";
        str2 = "hello,world,java";
        String[] s1 = str1.split("\\|");//split 按"|" 切片 输出hello world java
        for (String s : s1) {
            System.out.print(s+" ");
        }
        String[] s2 = str2.split("\\,");//按 "," 切片,输出hello world java
        for (String s : s2) {
            System.out.print(s+" ");
        }
    }
}
posted @ 2021-07-15 16:54  Dudo1  阅读(54)  评论(0)    收藏  举报