java中String类型

 

1 char charAt(int index)
返回指定索引处的 char 值。
2 int compareTo(Object o)
把这个字符串和另一个对象比较。
3 int compareTo(String anotherString)
按字典顺序比较两个字符串。
4 int compareToIgnoreCase(String str)
按字典顺序比较两个字符串,不考虑大小写。
5 String concat(String str)
将指定字符串连接到此字符串的结尾。
6 boolean contentEquals(StringBuffer sb)
当且仅当字符串与指定的StringBuffer有相同顺序的字符时候返回真。
7 static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
8 static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String。
9 boolean endsWith(String suffix)
测试此字符串是否以指定的后缀结束。
10 boolean equals(Object anObject)
将此字符串与指定的对象比较。
11 boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。
12 byte[] getBytes()
 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
13 byte[] getBytes(String charsetName)
使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
14 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串复制到目标字符数组。
15 int hashCode()
返回此字符串的哈希码。
16 int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
17 int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
18 int indexOf(String str)
 返回指定子字符串在此字符串中第一次出现处的索引。
19 int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
20 String intern()
 返回字符串对象的规范化表示形式。
21 int lastIndexOf(int ch)
 返回指定字符在此字符串中最后一次出现处的索引。
22 int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
23 int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。
24 int lastIndexOf(String str, int fromIndex)
 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
25 int length()
返回此字符串的长度。
26 boolean matches(String regex)
告知此字符串是否匹配给定的正则表达式。
27 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
28 boolean regionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
29 String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
30 String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
31 String replaceFirst(String regex, String replacement)
 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串。
32 String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
33 String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串。
34 boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
35 boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
36 CharSequence subSequence(int beginIndex, int endIndex)
 返回一个新的字符序列,它是此序列的一个子序列。
37 String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
38 String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
39 char[] toCharArray()
将此字符串转换为一个新的字符数组。
40 String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
41 String toLowerCase(Locale locale)
 使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。
42 String toString()
 返回此对象本身(它已经是一个字符串!)。
43 String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
44 String toUpperCase(Locale locale)
使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
45 String trim()
返回字符串的副本,忽略前导空白和尾部空白。
46 static String valueOf(primitive data type x)
返回给定data type类型x参数的字符串表示形式。
47 contains(CharSequence chars)
判断是否包含指定的字符系列。
48 isEmpty()
判断字符串是否为空。

 

1.获取
根据索引获取字符串中字符
返回值类型 方法名 参数
char           charAt  (int index)
根据字符获取在字符串中的位置
int i            ndexof  (int ch)
int              indexof (int ch, int fromIndex)
拼接字符串
String        concat (String string1)
2.判断
是否包含这个字符串 contains(CharSequence s)
是否以这个前缀开头 startWith(String prefix)

是否以这个后缀结尾 endWith(String suffix)

public class Demo02{
    public static void fun3(){
        String string = "www.baidu.com";
        boolean b1 = string.contains("baidu");
        System.out.println(b1);   //结果为:true
        //  前缀
        boolean b2 = string.startWith("www");
        System.out.println(b2);    //结果为:true
        //  后缀  .jpg .png .jpeg判断上传得图片是否合理
        boolean b3 = string.endWith("com");
        System.out.println(b3);    //结果为:true
    }
    
    public static void fun2(){
        //  包含
        String str1 = "wanglong";
        boolean b1 = str1.contains("an");
        System.out.println(b1);  //结果为:true
        boolean b2 = str1.contains("and");
        System.out.println(b2);   //结果为:false
        
        //  拼接字符串
        String str2 = "chinese";
        String str3 = str1 + str2;
        String str4 = str1.concat(str2);
        System.out.println(str3);  //结果为:wanglongchinese
        System.out.println(str4);  //结果为:wanglongchinese
    }
    
    public static void fun1(){
        //  传入索引返回对应字符
        String string1 = "wanglong";
        char c = string1.charAt(1);
        System.out.println(c);   //结果为:a
        //  传入字符,在字符串中0下标开始寻找下标
        int index = string1.indexOf('g');
        System.out.println(index); //结果为:7   如果找不到会返回-1
        //  从2下标开始寻找字符n的下标
        int index1 = string1.index('n',2);  //结果为:2
        //  传入字符串 查找对应位置
        int index2 = string1.indexOf("ng");
        System.out.println(index2);   //结果为:2
    }
    public static void main(String[] args){
        fun1();
        fun2();
        fun3();
    }
}

替换 切割 窃取子串 转换大小写 去空格 比较

判断两个字符串相等(忽略大小写)

把字符数组转化为字符串

把字符串转化为字符数组

判断字符串是否为空

public class Demo03{
    //  替换某个字符 或 字符串
    public static void fun1(){
        String string = "wanglong";
        String string1 = string.replace('a','f');   //有两个a的话,也都会被替换掉
        System.out.printlf(string1);  //输出结果:wfnglong
        
        String string2 = string.replace("ng","chinese"); // 有两个ng的话,也都会被替换掉
        System.out.println(string2);  //输出结果:wachineselong
    }
    
    //  获取子字符串
    public static void fun2(){
        String string = "wang,long,zui,niu,bi";
        //按逗号进行切割
        String[] array = string.split(",");
        for(String str : array){
            System.out.print(str + " ");  //输出结果为:wang long zui niu bi
        }
    }
    
    //  截取字符串
    public static void fun3(){
        String string = "wanglong";
        //  返回从索引往后的字符串
        String string1 = string.subString(5);
        System.out.println(string1);  //输出结果:ong
        //  类似random 截取时留头不留尾
        String string2 = string.subString(2,6);   //输出结果: nglo
    }
    
    //   转换大小写
    public static void fun4(){
        String string = "wanglong";
        //  转大写
        String string1 = string.toUpperCase();
        String string2 = "CHINESE";
        //  转小写
        String string3 = string2.toLowerCase();
        System.out.println(string1);  //输出结果:WANGLONG
        System.out.println(string3);  //输出结果:chinese
    }
    
    //  去空格
    public static void fun5(){
        String string = "   wang   long   ";
        String string1 = string.trim();
        System.out.println(string1);  //输出结果:wang   long
    }
    
    //  字符串比较
    /*
     *字符串相等的时候 返回0
     *两个字符串 长度相等时 一位一位比较  长度不相等 返回的是长度的差值
     */
     public static void fun6(){
         String string1 = "asd";
         String string2 = "asd";
         int number = string.compareTo(s2);
         System.out.println(number);   //输出结果:0
         
         String string3 = "asdf";
         String string4 = "asd";
         int number1 = string3.compareTo(string4);
         System.out.println(number1);  //输出结果为:1
         
         String string5 = "as";
         String string6 = "asd";
         int number2 = string5.compareTo(string6);   
         System.out.pritnln(number2);   //输出结果:-1
         
         String string7 = "asd";
         String string8 = "asf"
         int number3 = string7.compareTo(string8);  
         System.out.println(number3);   //输出结果:-2   两者的ASCII值相减
         
         char c = "轩";
         System.out.println((int)c);   //输出结果: 36713   Unicode码 ASCII码是Unicode码的一部分
    }
    
    //判断两个字符串是否相等 忽略大小写
    public static void fun7(){
        String string1 = "WangLong";
        String string2 = "wanglong";
        boolean bool = string1.equalsIgnoreCase(String2);
        System.out.println(b);  //输出结果为:true
    }
    
    /*
     * 把字符数组转为字符串
     * 把字符串转为字符数组
     * 判断字符串是否为空
*/
    public static void fun8(){
        char[] array = {a,c,d,w,f};
        String arr = new String(array);
        System.out.println(arr);  //输出结果为:acdwf
        
        //  int 型转字符串
        int num = 4;
        String string = String.valueOf(num);
        System.out.println(num); //输出字符串结果:4
        
        //  字符串转数组
        String string1 = "wanglong";
        char[] charArray = string1.toCharArray();
        for(char c : charArray){
            System.out.println(c);  //  输出结果 : w a n g l o n g
        }
        
        //判断字符串是否为空
        String string2 = "wanglong";
        boolean b = string3.isEmpty();
        System.out.println(b);    //输出结果: false
       
        //  获取字符串长度
        System.out.println(string2.length());   //输出结果:8
    }
    
    public static void main(String[] args){
        fun1();
        fun2();
    }
}

 

posted @ 2021-05-05 14:18  GGood_Name  阅读(37)  评论(0)    收藏  举报