Java基础(常用String类函数)

1.String类的构造方法

  • 常用的构造方法

 public String():空构造

 public String(byte[] bytes):把字节数组转成字符串

 public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串

 public String(char[] value):把字符数组转成字符串

 public String(char[] value,int index,int count):把字符数组的一部分转成字符串,index第一位开始取(包括),count取多少个

 public String(String original):把字符串常量值转成字符串

 length() 返回此字符串的长度。

  • 代码示例
package day0601;
public class demo01 {
    public static void main(String[] args) {
        String s1=new String();
        s1="aaa";
        System.out.println(s1);
        byte[] bytes={96,97,98,99,100};
        //将数字转换为ASCII码对应的字符值
        String s2=new String(bytes);
        System.out.println("s2:"+s2);
        //截取字符串的一部分
        String s3=new String(bytes,0,3);
        System.out.println("s3:"+s3);
        char[] chars={'a','b','c'};
        //将字符数组转化为字符串
        String s4=new String(chars);
        System.out.println("s4:"+s4);
        String s5=new String(chars,0,2);
        System.out.println("s5:"+s5);
        String s6=new String("abc");
        System.out.println("s6:"+s6);
        System.out.println("字符串的长度是:"+s6.length());
    }
}

2.String类的判断功能

  • 常用的判断方法

boolean equals(Object obj):比较字符串的内容是否相同,区分大小写

boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写

boolean contains(String str):判断大字符串中是否包含小字符串

boolean startsWith(String str):判断字符串是否以某个指定的字符串开头

boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾

boolean isEmpty():判断字符串是否为空。

  • 代码示例:
package day0601;
//字符串的判断功能
public class demo02 {
    public static void main(String[] args) {
        String s1 = "abc";
        String s2 = "Abc";
        //判断字符串是否相等
        System.out.println(s1.equals(s2));
        //比较字符串,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s2));
        //判断大字符串中是否包含小字符串
        System.out.println(s1.contains("ab"));
        //判断字符串是否一某个字符串开头
        System.out.println(s1.startsWith("ab"));
        //判断字符串是否一某个字符串结尾
        System.out.println(s1.endsWith("bcd"));
        //判断字符串是否为空
        System.out.println(s1.isEmpty());
    }
}

3.String类的获取功能

  • 常用的获取方法

    char charAt(int index):获取指定索引位置的字符

    int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引

    int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引

    int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。

    int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。

    String substring(int start):从指定位置开始截取字符串,默认到末尾。

    String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

  • 代码示例:
package day0601;
//字符串的获取功能
public class demo03 {
    public static void main(String[] args) {
        String s1 = "abcd_bcde";
        //返回指定索引位置的字符
        char c1 = s1.charAt(1);
        System.out.println(c1);
        //返回指定字符第一次出现的索引位置
        int i1 = s1.indexOf(97);
        System.out.println(i1);
        int i2 = s1.indexOf("a");
        System.out.println(i2);
        int i3 = s1.indexOf("bc");
        System.out.println(i3);
        //从指定位置开始截取字符串
        System.out.println(s1.substring(5));
        //从指定位置开始,截取指定个数的字符串
        System.out.println(s1.substring(1,6));
    }
}

4.String类的转换功能

  • 常用的转换方法

    byte[] getBytes():把字符串转换为字节数组。

    char[] toCharArray():把字符串转换为字符数组。

    static String valueOf(char[] chs):把字符数组转成字符串。

    static String valueOf(int i):把int类型的数据转成字符串。

    String toLowerCase():把字符串转成小写。

    String toUpperCase():把字符串转成大写。

    String concat(String str):把字符串拼接。

  • 代码示例:
package day0601;
import java.util.Arrays;

//字符串的转换功能
public class demo04 {
    public static void main(String[] args) {
        String s1="abcd";
        //把字符串转化为字节数组
        byte[] bytes=s1.getBytes();
        System.out.println(Arrays.toString(bytes));
        //把字符串转化为字符数组
        char[] chars=s1.toCharArray();
        System.out.println(Arrays.toString(chars));
        //把字符数组转换为字符串
        char[] c2={'a','b','c'};
        System.out.println(String.valueOf(c2));
        //把int型的数据转化成字符串
        String a=String.valueOf(123);
        System.out.println(a);
        //把字符串转化为小写
        String s2="ABCD";
        System.out.println(s2.toLowerCase());
        //把字符串转化为大写
        String s3="aaa";
        System.out.println(s3.toUpperCase());

    }
}

5.String类的替换功能

  • 常用的替换方法

    String replace(char old,char new):把字符串中的某个字符用新的字符所替换

    String replace(String old,String new)把字符串中的某个字符串用新的来替换

    String trim() 去除字符串两空格

  • 代码示例:
package day0601;
//字符串的替换功能
public class demo05 {
    public static void main(String[] args) {
        String s1="wwtc";
        //字符串的替换功能
        String s2=s1.replace("ww","y");
        //用新的字符串代替旧的字符串
        System.out.println(s2);
        String s3=s1.replace('w','y');
        //用新的字符代替旧的字符
        System.out.println(s3);
        //去掉字符串两端的空格
        String m="   wtc    ";
        System.out.println(m.trim());
    }
}

 

发布了75 篇原创文章 · 获赞 164 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_41679818/article/details/92602766
今日推荐