java基础【String类】

1、String类:

        字符串:内容不可以改变的Unicode字符的序列,任何对字符串的修改都会生成一个新的字符串对象。底层使用了char[]来保存字符,字符串的处理和下标密切相关。

        String类:构造字符串对象,一个字符占用两个字节。

    @Test
    // 字符串内容不可以改变
    public void test1(){
        String s1 = "abc";
        s1 = s1 + 100;  //产生一个新的字符串
        System.out.println(s1);
    }

2、几个构造器

@Test
    public void test2(){
        char[] arr = {'1','大','v','!'};

        String s1 = new String(arr); //将字符数组直接变成字符串
        System.out.println(s1);

        String s2 = new String(arr,2,1); //把arr中的一部分字符变成字符串,从arr中第2个元素开始,一共取出1个
        System.out.println(s2);
    }

3、字符串的特性

         以上s2和s3为false,其余都为true,其中intern方法作用为将GC区(存变量的区域)中的内容变为常量区中内容(变为常量提高系统效率),所以s5和s6相等,都指向常量区中的相同一块内容,equal()方法不比较地址,只比较字符串的内容。

4、一些对字符串的操作

String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";
public int length() 获取字符串长度(字符数) string.length() => 40
public char charAt(int index) 获取参数指定的下标位置处的字符 string.charAt(10) => y. string.charAt(13) => 喜
public char[] toCharArray() 获取字符串相应的字符数组, 是内部数组的一个副本
			System.arraycopy(value, 0, result, 0, value.length);
			// 第一个参数是源数组, 
			 * 第2个参数是源数组开始下标
			// 第3个参数是目标数组, 
			 * 第4个参数是目标数组的开始复制的下标, 
			 * 第5个参数是总共要复制的元素个数.
			 * 
			效果相当于 : 
			for (int i = 0; i < value.length; i++) {
				result[i] = value[i];
			}
			
public boolean equals(Object anObject)
public int compareTo(String anotherString)

String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";

public int indexOf(String s), 获取参数中的子串在当前字符串中首次出现的下标值 string.indexOf("喜欢") => 13, 如果搜索失败返回-1
public int indexOf(String s ,int startpoint) 获取第2个喜欢 : string.indexOf("喜欢", 14) =>         
  18, 
获取第3个喜欢 : string.indexOf("喜欢", 19) => 25,
												  
public int lastIndexOf(String s) 从右向左搜索子串出现的下标, string.lastIndexOf("喜欢") => 25
public int lastIndexOf(String s ,int startpoint) 获取第2个喜欢 : string.lastIndexOf("喜欢", 24) => 18
	
	// 通常获取文件列表名, 对文件名进行判断
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(12, 16) => "我喜欢你",  结束下标-开始下标 == 子串长度
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) 比较字符串的内容, 忽略大小写

求表长,根据下标获取字符串中的某一个字符,遍历字符串,翻转字符串:

@Test
    public void test3(){
        String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";
        System.out.println(string.length());  //求表长
        System.out.println(string.charAt(2)); //求指定位置的字符 ==string[2]

        //使用charAt()方法遍历字符串
        for(int i = 0; i < string.length(); i++){
            char ch = string.charAt(i);
            System.out.print(ch);
        }
        System.out.println();

        //翻转字符串
        for(int i = string.length(); i > 0; i--){
            char ch = string.charAt(i-1);
            System.out.print(ch);
        }
        System.out.println();
    }

正序、倒序进行检索关键字,检索短串在长串中出现的次数:

@Test
    public void test4(){
        String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";

        System.out.println(string.indexOf("喜欢"));           //从左向右搜索第一个“喜欢“
        System.out.println(string.indexOf("喜欢",14));    //在指定位置,从左向右搜索第一个”喜欢“

        System.out.println(string.lastIndexOf("喜欢"));     //从右向左搜索第一个“喜欢“
        System.out.println(string.lastIndexOf("喜欢",20));    //在指定位置,从右向左搜索第一个”喜欢“

        //利用indexOf()方法获取短串在长串中出现的次数
        String s1 = "abfjdksjkabfdnabfjdkabfd";
        String s2 = "ab";

        int count = 0;
        int index = 0;
        while(true){
            int targetIndex = s1.indexOf(s2, index);    //获取targetIndex
            if(targetIndex == -1){
                break;
            }
            count++;
            index = targetIndex + 1;            //在targetIndex + 1 的位置继续进行搜索
        }
        System.out.println(count);
    }

判断字符串是否以参数作为开始、结束。应用:获取文件名后,对文件类型进行判断,比如判断是否为.mp3类型文件

public void test5(){
        String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";
        System.out.println(string.startsWith("1"));
        System.out.println(string.startsWith("  "));

        System.out.println(string.endsWith("1"));
        System.out.println(string.endsWith("  "));
    }

从当前字符串中截取子串(前闭后开)

public void test6(){
        String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";
        System.out.println(string.substring(3,6));  //前闭后开,包含前面(3),不包含后面(6)
        System.out.println(string.substring(3,string.length()));

        System.out.println(string.substring(12));
    }

将一个字符串的部分翻转

    @Test
    public void exer(){
        //将abcdef翻转为abdcef
        String s = "abcdef";
        int begin = 2;
        int end = 4;
        String s1 = s.substring(0,begin);
        String s2 = s.substring(begin,end);
        String s3 = s.substring(end);

        char[] arr = s2.toCharArray();
        for(int i = 0; i < arr.length / 2; i++){
            char temp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;
        }
        s2 = new String(arr);
        System.out.println(s1 + s2 + s3);

//        String temps = new String();
//        for(int i = s2.length(); i > 0; i--){
//            char ch = s2.charAt(i - 1);
//            temps += ch;
//        }
//        System.out.println(s1  + temps + s3);

用新字符替换原串中所有旧字符,修改字符串首尾的空白字符(码值小于等于32的字符),应用:在获取用户的输入时,将一些不必要的干扰项删除掉,例如用户名。

@Test
    public void test7(){
        String string = "  abcABXXyy 我喜欢你,你喜欢我吗?我不喜欢你 qqyyZZ123  ";
        String replace = string.replace(' ','#');
        System.out.println(replace);        //以字符为单位

        String replaceAll = string.replaceAll("喜欢","不喜欢");
        System.out.println(replaceAll);     //以字符串为单位

        System.out.println(string.trim());  //修改字符串首尾的空白字符
    }

模拟trim方法

    @Test
    public void exer1(){
        String string = "\r\t\b\n  ds 你好 \n\t\b\n ";
        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(); i > 0; i--){
            char ch = string.charAt(i - 1);
            if(ch > 32){
                end = i;
                break;
            }
        }
        if(begin == 0 && end == 0){
            System.out.println("");
        }else{
            String substring = string.substring(begin,end + 1);
            System.out.println(substring);
        }
    }

将字符串中所有字符变为大写、小写字母,以参数(某种规则)对字符串进行切割,切割后是一个字符数组。

 @Test
    public void test8(){
        String string = "  abcABXXyy 我喜,欢你,你喜,欢我吗?我不喜,欢你 qqyyZZ123  ";
        System.out.println(string.toUpperCase());
        System.out.println(string.toLowerCase());
        String[] split = string.split(",");
        for(int i = 0; i < split.length ; i++){
            System.out.println(split[i]);
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值