008—JAVA中的String大类(详细知识要点—绝对够用)

1.String
    只要使用字符串包裹 就是字符串的一个实例
    实例=对象(可以使用"你好".equals()因为一个字符串就是一个对象)
2.String 是不可变的字符序列
     private final char value[];(底层被final修饰表示存储字符串底层使用char型数组进行存储的,被final修饰以后,表示地址不可变。)

3.final class String  不能有子类(不能被继承)

4. String 是java.lang 之下的资源  使用是不需要导包

字符串对象的创建

public void test01(){
        String s1 = "Hello";
    }
@Test
    public void test(){
        String hello = new String("Hello");
    }

 将byte[]数组(存储的是编码值)放入字符串中,打印的是对应的字符

可以尝试使用new String()不同的重载方法进行测试

@Test
    public void test2(){
        byte[] bs = {65,98,99,100};
        String s = new String(bs);
        System.out.println("s = " + s);

        String s1 = new String(bs,1,2);
        System.out.println("s1 = " + s1);

    }

 将char型数组转存为字符串类型

 @Test
    public void test3(){
        char [] cs = {'A','B','C','D'};
        String s = new String(cs);
        System.out.println("s = " + s);

        String s1 = new String(cs, 1, 2);
        System.out.println(s1);

    }

使用valueOf()方法或者copyValueOf()方法转换成字符串

@Test
    public void test4(){
       int m = 10;
       String s = "" + m;
        System.out.println(s);
        String s1 = String.valueOf(m);

        String s2 = String.valueOf(3.14);

        char[] cs = {'A','B','C','D'};
        String s3 = String.copyValueOf(cs);
        System.out.println(s3);

    }

字符串的常用方法:

1>  isEmpty():  判空
2>  length():   数组长度方法
3>  concat()    拼接(字符串不可变 拼接之后生成新的对象)
4>  toUpperCase()/toLowerCase()  转成大小写  生成的是新的对象
5>  equals()    重写了Object类中的equals方法,比较的是内容

 

@Test
    public void test1(){
        String s = "";
        boolean empty = s.isEmpty();
        System.out.println(empty);

        String s1 = "ABCDE";
        System.out.println("s1.length() = " + s1.length());
    }
 @Test
    public void test2(){
        String s1 = "Hello ";
        String s2 = "World";

        String s3 = s1 + s2;
        String s4 = s1.concat(s2);

    }
@Test
    public void test3(){
        String s1 = "abcdEFG中";
        String s = s1.toUpperCase();
        System.out.println("s = " + s);
        System.out.println("s1 = " + s1);

    }
@Test
    public void test4(){
        String s1 = new String("ABC");
        String s2 = "ABC";

        System.out.println("s1==s2 = " + s1 == s2);
        System.out.println("s1.equals(s2) = " + s1.equals(s2));


        String s3 = "AbC";
        System.out.println("s2.equals(s3) = " + s2.equals(s3));


    }
 @Test
    public void test5(){
       String s = "A";
       String s1 = "D";
        //按照对应的编码值进行比较
        int i = s.compareTo(s1);
        System.out.println("i = " + i);

        String s2 = "a";
        int i1 = s.compareTo(s2);
        System.out.println("i1 = " + i1);

        int i2 = s.compareToIgnoreCase(s2);
        System.out.println("i2 = " + i2);

    }

String常用方法二

1>   compareTo()  进行对象比较

String底层实现了Comparable接口,因此可以进行对象间的比较implements java.io.Serializable, Comparable<String>, CharSequence

2>  trim()   去除字符串前后空格

3>  indexOf()/lastIndexOf()     获取对应字符串下标

4>  contains()        字符串中是否包含所找的字符串

5>   toCharArray()         将字符串编程char型数组      

 

 @Test
    public void test(){
        String s = "A";
        String s1 = "D";
        //按照对应的编码值进行比较
        int i = s.compareTo(s1);
        System.out.println("i = " + i);

        String s2 = "a";
        int i1 = s.compareTo(s2);
        System.out.println("i1 = " + i1);

        int i2 = s.compareToIgnoreCase(s2);
        System.out.println("i2 = " + i2);
    }
@Test
    public void test1(){

        String s = "   A  b  MC   ";
        String s2 = "<---------->";
        System.out.println("(s2+s+s2) = " + (s2 + s + s2));

        String trim = s.trim();
        System.out.println(s2 + trim + s2);

    }
@Test
    public void test2(){
        //指定的字符串第一次出现的下标
        //没有查找的数据时候返回-1;
        String s = "ABCDEFBG";
        int index = s.indexOf("B");
        System.out.println("index = " + index);

        int index1 = s.indexOf("B", 2);
        System.out.println("index1 = " + index1);
        //最后依次出现的下标  没有的话 返回-1
        int index2 = s.lastIndexOf("F");
        System.out.println("index2 = " + index2);


    }
@Test
    public void test3(){
        String s = "ABCFDEFG";
        boolean cf = s.contains("CF");
        System.out.println("cf = " + cf);

    }
@Test
    public void test4(){
        String s = "ABCFDEFG";
        char c = s.charAt(3);
        System.out.println("c = " + c);
        char[] chars = s.toCharArray();
        String string = Arrays.toString(chars);
        System.out.println("string = " + string);
    }
 @Test
    public void test5(){
        //字符串遍历
        String s = "ABCFBG";
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            System.out.println("c = " + c);
        }
    }

Sting方法练习题

AAAABBBBCCCDDZZXXXaaakkk

AAAABBBBCCC中国中国
AAAABBBBCCC中中国国
每一个字母出现的次数

 解法一(有局限性)

public class Test {
    public static void main(String[] args) {

        String str = "AAAABBBBCCCDDZZXXXaaakkk";
        //先获取所有的字母
        for (char i = 'A'; i <= 'z' ; i++) {
//            System.out.println(i);
            //遍历字符串
            int count = 0;

            for (int j = 0; j < str.length(); j++) {
                char c = str.charAt(j);
                if (i == c){
                   count++;
                }
            }
            if (count != 0){

            System.out.println(i + "出现的次数" + count);
            }
        }

    }
}

解法二:(AAAABBBBCCC中国中国 找到字符串内不重复的字符 ABC中国)

public class Test1 {
    public static void main(String[] args) {
        String str = "   AAAABBBBCCC中国中国";
        String str1 = "";
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
            char c = str.charAt(i);
            if (!str1.contains(c+"")){
                str1 += c;
            }
        }
        for (int i = 0; i < str1.length(); i++) {
            char c = str1.charAt(i);
            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                char c1 = str.charAt(j);
                if (c == c1){
                    count++;
                }
            }
            System.out.println(c + "出现的次数" + count) ;

        }
    }
}

解法三(排序)(AAAABBBBCCC中中国国 排序)

@Test
    public void test(){
        String str = "AABABABCBCBBCCCC中国aa中ss国zz";
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        String s1 = new String(chars);
        for (int i = 0; i < s1.length(); ) {
            //获取起始字符
            char start = s1.charAt(i);
            //获取结束下标
            int lastIndexOf = s1.lastIndexOf(start);
            //结束下标-开始下标+1
            int count = lastIndexOf - i + 1;
            System.out.println(start+" ---> "+count);
            //获取第二个元素的开始下标 = 结束下标+1
            i = lastIndexOf+1;

        }

    }

String常用方法三

1>        substring()  / substring(1, 4) 获取字串

2>        toCharArray()    将字符串转成char型数组

3>        getBytes() /  getBytes("GBK")   重点 获取底层编码值  也可以设置编码方式

4>        startsWith("E") /  endsWith("FG")

5>        split("\\d")          以数字为分隔符

6>          replace('B', 'z');      替换单个字符或替换字符串

@Test
    public void test(){
        String s = "ABCDEFG";
        String substring = s.substring(2);
        System.out.println("substring = " + substring);

        String substring1 = s.substring(1, 4);
        System.out.println("substring1 = " + substring1);
    }
@Test
    public void test1(){
        String s = "EFGABCD";
        char[] chars = s.toCharArray();
        //对数组进行排序
        Arrays.sort(chars);
        String string = Arrays.toString(chars);
        //将数组转为字符串
        String s1 = new String(chars);
        System.out.println("s1 = " + s1);
    }
@Test
    public void test2() {
        String s = "EFGABCD";
        //bytes可以将当前数值转换为对应的编码值
        byte[] bytes = s.getBytes();
        System.out.println("bytes.toString() = " + Arrays.toString(bytes));
    }
@Test
    public void test3() throws UnsupportedEncodingException {
        String s = "你好";
        //在utf-8下一个汉字需要三个字节
        byte[] bytes = s.getBytes();
        String string = Arrays.toString(bytes);
        System.out.println("string = " + string);

        String s1 = "世界";
        //gbk编码下一个汉字两个字节
        byte[] gbks = s1.getBytes("GBK");
        System.out.println("gbks.length = " + gbks.length);

        System.out.println("------------------");
        //编码不一致乱码
        String s2 = new String(gbks, "utf-8");
        System.out.println("s2" + s2);
    }
@Test
    public void test5(){
        String s = "EFGABCD";
        boolean ab = s.startsWith("E");
        System.out.println("ab = " + ab);

        boolean fg = s.endsWith("FG");
        System.out.println("fg = " + fg);
    }
@Test
    public void test6(){
        String s = "A1B1C1D1E1F1";
        //分割
        String[] split = s.split("1");
        String string = Arrays.toString(split);
        System.out.println("string = " + string);
    }
@Test
    public void test7(){
        String str = "A1B2C3D4E5F6G";
        String[] split = str.split("\\d");
        String string = Arrays.toString(split);
        System.out.println("string = " + string);
    }
@Test
    public void test8(){
        String str = "A1B2C3D4E5F6G";
        String replace = str.replace('B', 'z');
        System.out.println("replace = " + replace);

        String replace1 = str.replace("2C", "你好世界");
        System.out.println(replace1);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值