Java(String类)

String类:构造字符串对象

常量对象:字符串常量对象是用双引号括起的字符序列

字符串的字符使用Unicode字符编码,一个字符占两个字节

String类较常用构造方法:
 

String  s1 = new String();

String  s2 = new String(String original);

String  s3 = new String(char[] a);//把字符数组直接变成字符串

String  s4 =  new String(char[] a,int startIndex,int count);//把字符数组的一部分变成字符串,第二个参数是开始下标,第三个参数是取几个字符
package com.day17.string;

import org.junit.Test;

import java.util.Arrays;

/*
        字符串:内容不可改变的Unicode字符的序列,任何的对字符串的修改都一定会产生新的字符串对象。
        底层使用了char[]来保存字符,字符穿的处理和下标密切相关
                         0123456789 11                     25           37
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 "
        *****
        public int length() 获取字符串长度(字符数)string.length()=>40
        *****
        public char charAt(int index) 获取参数指定的下标位置处的字符string.charAt(9) => y  string.charAt(11) => 吃
        public char[] toCharArray() 获取字符串相应的字符数组,是内部数组的一个副本
                            System.arraycopy(value,0,result,0,value.length);
                            第一个参数是目标数组,
                            第二个参数是源数组开始下标
                            第三个参数是目标数组,
                            第四个参数是目标数组的开始复制的下标,
                            第五个参数是总共要复制的元素个数。
                            效果相当于:
                            for(int i = 0, i < value.length; i++){
                                result[i] = value[i];
                                }
        ****
        public boolean equals(Object anObject)
        public int compareTo(String anotherString)
        ***
        public int indexOf(String s) 获取参数中的子串在当前字符串中首次出现的下标值string.indexOf("吃了") => 11 ,如果搜索失败返回-1
        public int indexOf(String s ,int startpoint) 获取第二个吃了:string.indexOf("吃了",12) => 20
                                                     获取第三个吃了:string.indexOf("吃了",21) => 25

        public int lastIndexOf(String s) 从右向左搜索子串出现的下标, string.lastIndexOf("吃了") => 25
        public int lastIndexOf(String s ,int startpoint) 获取第二个吃了:string.lastIndexOf("吃了",24) => 20
                                                         获取第三个吃了:string.lastIndexOf("吃了",19) => 11
        // 通常获取文件列表名, 对文件名进行判断
        *
        public boolean startsWith(String prefix)判断字符串是否以参数中的子串为开始 string.startsWith("  abc") => true
	    public boolean endsWith(String suffix) 判断字符串是否以参数中的子串为结束 string.endsWith("123") => false
        *****
        public String substring(int start,int end) 从当前字符串中截取子串, start表示开始下标(包含), end表示结束下标(不包含)
						string.substring(11, 14) => "吃了么",  结束下标-开始下标 == 子串长度
        public String substring(int startpoint)从当前字符串取子串,从start开始到结束

        public String replace(char oldChar,char newChar) 替换旧字符为新字符
        public String replaceAll(String old,String new)全部替换老串为新串, 特殊字符 \ [ * +

        public String trim() 修建字符串的首尾的空白字符(码值小于等于32的字符)
        public String concat(String str)

        public String toUpperCase()改变大小写
        public String toLowerCase()
        public String[] split(String regex) 以参数中的子串为切割器, 把字符串切割成多个部分.
        *****
        public boolean equalsIgnoreCase(String s2) 比较字符串的内容, 忽略大小写

 */
public class StringTest {
    @Test
    public void  test12(){
        String s1 = "abc";
        String s2 = "ABC";
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));
    }
    @Test
    public void exercise8(){
        String path = "PATH=D:\\mywork\\Program\\jdk1.8.0_221\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\Common Files\\Autodesk Shared\\;";
        String[] split = path.split(";");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);

        }
    }
    @Test
    public void test11() {
        String string = "abc,你好吗,不好,12345,qqqq";
        String[] split = string.split("好");
        for (int i = 0; i <split.length ; i++) {
            System.out.println(split[i]);
        }

    }
    @Test
    public void test10() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String s = string.toUpperCase();
        String s1 = string.toLowerCase();
        System.out.println(s);
        System.out.println(s1);

    }
    //模拟一个trim方法,去除字符串两端的空白字符
    @Test
    public void exercise7() {
        String string = " \t\n\r  \t\t\r \n asdasdjaslkdjlk 哈哈哈 0123\n\f\r\b";
        int begin = 0;
        int end = 0;
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (ch > 32) {
                begin = i;
                break;
            }
        }
        for (int i = string.length() - 1; i >= 0; i--) {
            char ch = string.charAt(i);
            if (ch > 32) {
                end = i;
                break;
            }
        }
        if (begin == 0 && end == 0) {
            System.out.println("");
        }
        String substring = string.substring(begin, end + 1);
        System.out.println(substring);
    }

    public void exercise6() {
        String string = " \t\n\r  \t\t\r \n asdasdjaslkdjlk 哈哈哈 0123\n\f\r\b";
        int begin = string.indexOf("a");
        int end = string.lastIndexOf("3");
        String substring = string.substring(begin, end + 1);
        System.out.println(substring);
    }

    @Test
    public void test9() {
        String string = " \t\n\r  \t\t\r \n asdasdjaslkdjlk 哈哈哈 0123\n\f\r\b";
        System.out.println(string);
        String trim = string.trim();
        System.out.println(trim);

    }

    @Test
    public void test8() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String replace = string.replace(' ', '@');
        System.out.println(replace);

        String s = string.replaceAll("吃了", "饿了");
        System.out.println(s);
        //消除字符串中所以空格
        String s1 = string.replaceAll(" ", "");
        System.out.println(s1);

    }

    //将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为”abfedcg”
    public static String partReverse(String string, int begin, int end) {
        String s1 = string.substring(0, begin);
        String s2 = string.substring(begin, end);
        String s3 = string.substring(end);
        //只反转中间的部分
        char[] chars = s2.toCharArray();
        for (int i = 0; i < chars.length / 2; i++) {
            char tmp = chars[i];
            chars[i] = chars[chars.length - 1 - i];
            chars[chars.length - 1 - i] = tmp;
        }
        s2 = new String(chars);
        //再拼接
        String result = s1 + s2 + s3;
        return result;
    }

    @Test
    public void test7() {
        String str1 = "abcdefghijklmn";
        int begin = 2;//包含
        int end = 6;//不包含
        System.out.println(partReverse(str1, begin, end));

    }


    @Test
    public void test6() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String sub = string.substring(11, 14);
        System.out.println(sub);
        String sub2 = string.substring(25, string.length());//从25开始一直取到末尾
        System.out.println(sub2);

        String sub3 = string.substring(25);
        System.out.println(sub3);
    }

    @Test
    public void test5() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        System.out.println(string.startsWith(" abc"));
        System.out.println(string.endsWith("123"));
    }

    /*
       获取一个字符串在另一个字符串中出现的次数。
       比如:获取"ab"在 "abkkcadkabkebfkabkskab"
       中出现的次数
      */
    public static int getCount(String s1, String s2) {
        if (s2.length() > s1.length()) {
            String tmp = s1;
            s1 = s2;
            s2 = tmp;
        }
        int count = 0;
        int index = 0;
        while (true) {
            index = s1.indexOf(s2, index);
            if (index == -1) {
                break;
            }
            count++;
            index++;
        }
        return count;
    }

    @Test
    public void exercise5() {

        String s1 = "abkkcadkabkebfkabkskab";
        String s2 = "ab";
        System.out.println(getCount(s2, s1));

    }

    @Test
    public void test4() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        int indexOf = string.indexOf("qq");
        System.out.println(indexOf);
        int indexOf2 = string.indexOf("好");
        System.out.println(indexOf2);
    }

    @Test
    public void exercise4() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String str = "";
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            str = ch + str;
        }
        System.out.println(str);
    }

    @Test
    public void exercise3() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String str = "";
        for (int i = 0; i < string.length() - 1 - i; i++) {
            char ch = string.charAt(string.length() - 1 - i);
            str += ch;
        }
        System.out.println(str);
    }

    @Test
    public void exercise2() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        char[] c1 = string.toCharArray();
        for (int i = 0; i < c1.length / 2; i++) {
            char tmp = c1[i];
            c1[i] = c1[c1.length - 1 - i];
            c1[c1.length - 1 - i] = tmp;
        }
        String str = new String(c1);
        System.out.println(c1);
    }

    @Test
    public void exercise1() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        String str = "";
        for (int i = string.length() - 1; i >= 0; i--) {
            char ch = string.charAt(i);
            str += ch;
        }
        System.out.println(str);

    }

    @Test
    public void test3() {
        String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 ";
        System.out.println(string.length());//获取字符串长度(字符数)
        System.out.println(string.charAt(5));//string[5]

        //遍历字符串
        for (int i = 0; i < string.length(); i++) {
            System.out.println(string.charAt(i));
        }
    }

    @Test
    public void test2() {
        String s2 = new String();
        String s3 = "";

        char[] arr = {'a', 'b', 'c', 'd', '点'};
        String s4 = new String(arr);//把字符数组直接变成字符串
        System.out.println(s4);

        String s5 = new String(arr, 2, 3);//把字符数组的一部分变成字符串,第二个参数是开始下标,第三个参数是取几个字符
        System.out.println(s5);
    }


    @Test
    public void test1() {
        String s1 = "abc";
        s1 = s1 + 100;// s1 + 100产生一个新的字符串

    }

}
 字符串:内容不可改变的Unicode字符的序列,任何的对字符串的修改都一定会产生新的字符串对象。
   底层使用了char[]来保存字符,字符穿的处理和下标密切相关
                    0123456789 11                     25           37
   String string = " abcABXXyy 吃了么,没吃呢,你吃了么? 吃了 qqyyZZ123 "
   *****
   public int length() 获取字符串长度(字符数)string.length()=>40
   *****
   public char charAt(int index) 获取参数指定的下标位置处的字符string.charAt(9) => y  string.charAt(11) => 吃
   public char[] toCharArray() 获取字符串相应的字符数组,是内部数组的一个副本
                       System.arraycopy(value,0,result,0,value.length);
                       第一个参数是目标数组,
                       第二个参数是源数组开始下标
                       第三个参数是目标数组,
                       第四个参数是目标数组的开始复制的下标,
                       第五个参数是总共要复制的元素个数。
                       效果相当于:
                       for(int i = 0, i < value.length; i++){
                           result[i] = value[i];
                           }
   ****
   public boolean equals(Object anObject)
   public int compareTo(String anotherString)
   ***
   public int indexOf(String s) 获取参数中的子串在当前字符串中首次出现的下标值string.indexOf("吃了") => 11 ,如果搜索失败返回-1
   public int indexOf(String s ,int startpoint) 获取第二个吃了:string.indexOf("吃了",12) => 20
                                                获取第三个吃了:string.indexOf("吃了",21) => 25

   public int lastIndexOf(String s) 从右向左搜索子串出现的下标, string.lastIndexOf("吃了") => 25
   public int lastIndexOf(String s ,int startpoint) 获取第二个吃了:string.lastIndexOf("吃了",24) => 20
                                                    获取第三个吃了:string.lastIndexOf("吃了",19) => 11
   // 通常获取文件列表名, 对文件名进行判断
   *
   public boolean startsWith(String prefix)判断字符串是否以参数中的子串为开始 string.startsWith("  abc") => true
public boolean endsWith(String suffix) 判断字符串是否以参数中的子串为结束 string.endsWith("123") => false
   *****
   public String substring(int start,int end) 从当前字符串中截取子串, start表示开始下标(包含), end表示结束下标(不包含)
   string.substring(11, 14) => "吃了么",  结束下标-开始下标 == 子串长度
   public String substring(int startpoint)从当前字符串取子串,从start开始到结束

   public String replace(char oldChar,char newChar) 替换旧字符为新字符
   public String replaceAll(String old,String new)全部替换老串为新串, 特殊字符 \ [ * +

   public String trim() 修建字符串的首尾的空白字符(码值小于等于32的字符)
   public String concat(String str)

   public String toUpperCase()改变大小写
   public String toLowerCase()
   public String[] split(String regex) 以参数中的子串为切割器, 把字符串切割成多个部分.
   *****
   public boolean equalsIgnoreCase(String s2) 比较字符串的内容, 忽略大小写

字符串转换为基本数据类型

Integer包装类的public static int parseInt(String s):可以将由“数字”字符组成的字符串转换为整型。

类似地,使用java.lang包中的Byte、Short、Long、Float、Double类调相应的类方法可以将由“数字”字符组成的字符串,转化为相应的基本数据类型。

基本数据类型转换为字符串

调用String类的public String valueOf(int n)可将int型转换为字符串 相应的valueOf(byte b)、valueOf(long l)、valueOf(float f)、valueOf(double d)、valueOf(boolean b)可由参数的相应类到字符串的转换

Unicode每个字符占两个字节

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值