String的常用方法

  1. String(byte[] bytes):是一个字节数组转换成字符串。byte是数组类型,bytes是数组名字。
  2. String(byte[] bytes,int index,int
    length):是讲一个字节数组的一部分转换成字符串,其中,index是开始索引数,length是转换长度。
  3. String(char[] chars):将一个字符数组转换成字符串。第一个char是数组类型,第二个chars是数组名字。
  4. String(char[] chars,int index,int
    length):是讲一个字符数组的一部分转换成字符串,其中,index是开始索引数,length是转换长度。
public class Test1 {
    public static void main(String[] args) {
        byte[]b1={'a','b','c','d','e','f'};
        String str1=new String(b1);
        System.out.println("字节数组转换成字符串"+str1);
        String str2 = new String(b1,2,4);
        System.out.println("字节数组的一部分转换成字符串:"+str2);
        char []ch={'s','d','f','f','q','w','k'};
        String s3=new String(ch);
        System.out.println("字符数组转换成字符串:"+s3);
        String s4 = new String(ch,2,4);
        System.out.println("字符数组的一部分转换成字符串:"+s4);
    }
}
运行结果:
字节数组转换成字符串abcdef
字节数组的一部分转换成字符串:cdef
字符数组转换成字符串:sdffqwk
字符数组的一部分转换成字符串:ffqw

equals(Object obj):判断字符串字面值是否相同(区分大小写)。
equalsIgnoreCase(Object obj):判断字符串字面值是否相同(不区分大小写)。
public class Test1 {
    public static void main(String[] args) {
        String s1="abc";
        String s2="abc";
        String s3="Abc";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equalsIgnoreCase(s3));
    }
}
运行结果:
true
false
true
  1. contains(char chars):判断字符串是否包含某字节chars。
  2. contains(String str):判断字符串是否包含某字符串str。
  3. startsWith(String str):判断字符串是否以某字符串开始。
  4. endsWith(String str):判断字符串是否以某字符串结束。
  5. isEmpty():判断字符串是否为空。
public class Test1 {
    public static void main(String[] args) {
        String s1="abcdef";
        String s2="Abc";
        System.out.println("s1中有字符d:"+s1.contains("d"));
        System.out.println("s1中包含字符串abc"+s1.contains("abc"));
        System.out.println("s1是以a开始的:"+s1.startsWith("a"));
        System.out.println("s2是以d结尾的:"+s2.endsWith("d"));
        System.out.println("s2是空串:"+s2.isEmpty());
    }
}
运行结果:
s1中有字符d:true
s1中包含字符串abctrue
s1是以a开始的:true
s2是以d结尾的:false
s2是空串:false
  1. length():获取字符串的长度,返回值为int型。
  2. charAt(int i):获取第i位的字符。返回值是char型。
  3. subString(int index):从指定位置index开始,截取剩余字符串。
  4. subString(int index,int
    length):提取指定字符串,index是起始索引,length是提取长度。返回值是String型。
  5. indexOf(char chars):获取第一个字节chars的索引。返回值是int型。
  6. indexOf(char chars,int
    fromIndex):从指定位置fromIndex开始,返回第一个字节chars的索引。返回值是int型。
  7. indexOf(String str):获取第一个字符串str的索引,默认是str第一个字节的索引。返回值是int型。
  8. indexOf(String str,int
    fromIndex):从指定位置formIndex开始,返回第一个字节str的索引。返回值是int型。
  9. lastIndexOf(char chars):从后往前获取第一个字节char的索引。返回值是int型。
public class Test1 {
    public static void main(String[] args) {
        String s1="abcdecf";
        System.out.println("s1的长度:"+s1.length());
        System.out.println("s1的第三个字符是:"+s1.charAt(2));
        System.out.println("s1从第四个位置开始截取:"+s1.substring(3));
        System.out.println("s1从第三个位置开始截取两个字符:"+s1.substring(2,2));
        System.out.println("返回s1中第一个c的索引"+s1.indexOf("c"));
        System.out.println("返回s1中,从3开始的第一个c的索引"+s1.indexOf("c", 3));
        System.out.println("返回s1的字符串“de”的索引,默认返回字符串中第一个字符的索引"+s1.indexOf("de"));
        System.out.println("返回s1中,从后往前第一个字符c的索引"+s1.lastIndexOf("c"));
    }
}
运行结果:
s1的长度:7
s1的第三个字符是:c
s1从第四个位置开始截取:decf
s1从第三个位置开始截取两个字符:
返回s1中第一个c的索引2
返回s1中,从3开始的第一个c的索引5
返回s1的字符串“de”的索引,默认返回字符串中第一个字符的索引3
返回s1中,从后往前第一个字符c的索引5
  1. getBytes():将字符串转换成字节数组。
  2. toCharArray():将字符串转换成字符数组。
  3. valueOf(char[] chs):将字符数组转换成字符串。
  4. valueOf(int i):将int类型的数据转换成字符串。
  5. toLowerCase():将字符串所有字母都变为大写。
  6. toUpperCase():将字符串所有字母都变为小写。
  7. concat(String str):拼接字符串,将字符串str拼到本字符串后面。
  8. replace(char old,char new):替换字符串中的字符。
  9. replace(String old,String new):替换字符串中的部分字符串。
public class Test1 {
    public static void main(String[] args) {
        String s1="abcdEFG";
        byte[]arr=s1.getBytes();
        System.out.println("输出字符串s1转的字节数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
        System.out.println();
        char[]ch=s1.toCharArray();
        System.out.println("输出s1转的字符数组:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(ch[i]+"\t");
        }
        System.out.println();
        System.out.println("输出s1转的字符串:");
        String s2=s1.valueOf(ch);
        System.out.println(s2);
        System.out.println("全部变为小写:"+s1.toLowerCase());
        System.out.println("全部变为大写:"+s1.toUpperCase());
        System.out.println("将“hij”加到后面:"+s1.concat("hij"));
        System.out.println("替换a字符:"+s1.replace('a', '*'));
        System.out.println("替换“EFG”字符串:"+s1.replace("EFG", "XYZ"));
    }
}
运行结果:
输出字符串s1转的字节数组:
97	98	99	100	69	70	71	
输出s1转的字符数组:
a	b	c	d	E	F	G	
输出s1转的字符串:
abcdEFG
全部变为小写:abcdefg
全部变为大写:ABCDEFG
将“hij”加到后面:abcdEFGhij
替换a字符:*bcdEFG
替换“EFG”字符串:abcdXYZ

去除空格:

  • String方法中,只给出了去除两端空格的方法:public String trim()
public class Test1 {
    public static void main(String[] args) {
        String s="   lf sj dl   ";
        System.out.println(s.trim());
    }
}
运行结果:
lf sj dl

但在实际应用中,我们可能会遇到其他情况。这里给出三种常见的去空格情况及解决方法。

  1. 去左边空格
package demo2;
//去除左端空格
public class Test1 {
    public static void main(String[] args) {
        String s="   lsafdj   fklsdff  ";
        int n=0;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)!=' '){
                n=i;
                break;
            }
        }
        System.out.println(s.substring(n));
    }
}
运行结果:
lsafdj   fklsdff  
  1. 去右边空格:
package demo2;
//去除右端空格
public class Test2 {
    public static void main(String[] args) {
        String s="  dfsjl  ldfdj    ";
        int n=0;
        for (int i = s.length() - 1; i>-1; i--) {
            if(s.charAt(i)!=' '){
                n=i;
                break;
            }
        }
        System.out.println(s.substring(0, n));
    }
}
运行结果:
  dfsjl  ldfd
  1. 去全部空格:
package demo2;
//去除全部空格
public class Test3 {
    public static void main(String[] args) {
        String s="  fdjs  lsdf   ";
        System.out.println(s.replace(" ", ""));
    }
}
运行结果:
fdjslsdf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值