Java筑基—String类

在这里插入图片描述


一、字符串的拼接

Java语言允许使用 + 号拼接两个字符串
注意:当将一个字符串与一个非字符串的值进行拼接时,后者被转换成字符串。

public class String_String {
    public static void main(String[] args) {
        //两个字符串进行拼接
        String s1="ko";
        String s2="be";
        System.out.println(s1+s2);
        //字符串 和 非字符串进行拼接
        String s3="age is ";
        int s4 = 18;
        // "整型"被转换成"字符串型"并与s3进行进行拼接
        System.out.println( s3 + s4 );
    }
}
kobe
age is 18

在这里插入图片描述

二、获取字符串长度

获取字符串长度:length()
获取字符串的长度
返回int类型

package com.company.StringDemo;

public class String_String {
    public static void main(String[] args) {
        String name = "helloworld";
        // 返回字符串的长度
        // 返回值是int类型
        int name_length=name.length();
        System.out.println(name.length());
        System.out.println(name_length);
        
    }
}

三、字符串转换

toLowerCase():所有字母小写的字符串
toUpperCase():所有字母大写的字符串

字符串的全部转换

package com.company.StringDemo;

public class String_String {
    public static void main(String[] args) {
        
        String a = "HelloWorld";
        String atoLowerCase = a.toLowerCase();
        System.out.println(atoLowerCase);

        String atoUpperCase = a.toUpperCase();
        System.out.println(atoUpperCase);

    }
}
helloworld
HELLOWORLD

四、去除前后空白字符

trim():删除字符串2端的空白字符返回一个新的字符串。
空白字符:" ",、\t,\f,\n,\r

package com.company.StringDemo;

public class String_String {
    public static void main(String[] args) {
        
        String b = " this is string ";
        System.out.println(b + "1");
        // 将原字符串 头部 和 尾部 的空格删除
        //空格 ' '、\t \f \n \r
        System.out.println(b.trim() + "1");    // this is string1
        System.out.println(b.length());        // this is string
        System.out.println(b.trim().length()); //14
        System.out.println("-----------------字符串去除空白字符---------");

        String b1 = "\f this is string \t";
        System.out.println(b1 + "1");
        System.out.println(b1.trim() + "1");
        System.out.println(b1.length());        // this is string
        System.out.println(b1.trim().length()); //14
    }
}
 this is string 1
this is string1
16
14
-----------------字符串去除空白字符---------
 this is string 	1
this is string1
18
14

五、比较字符串是否相等

equals(s1):将字符串与s1进行比较
忽略大小写比较:s1.equalsIgnoreCase(s2)
不能使用“==”运算符对两个字符串进行相等比较
==:只能确定两个字符串是否指向同一个对象
不会比较字符串的内容是否相等

package com.company.StringDemo;

public class String_String {
    public static void main(String[] args) {
        System.out.println("-------------比较字符串---------");
        //判断字符串完全相等,区分大小写
        String str1 = "HelloWorld";
        String str2 = "helloworld";
        System.out.println(str1.equals(str2));

        //判断字符串是否完全相等,不分区大小写
        System.out.println(str1.equalsIgnoreCase(str2));

        // == 不要用,string对应的内容是否相同

    }
}

六、比较字符串是否包含

package com.company.StringDemo;

public class String_String1 {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(str.contains("llo"));  //ture
        System.out.println(str.contains("qqq"));   //false

    }
}

七、字符串是否以某些开始、结尾

package com.company.StringDemo;

import java.sql.SQLSyntaxErrorException;

public class String_String1 {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(str.contains("llo"));  //ture
        System.out.println(str.contains("qqq"));   //false

        Boolean a = str.startsWith("Hello");
        boolean b = str.endsWith("world");
        System.out.println(a);
        System.out.println(b);
    }
}

八、字符串的替换

package com.company.StringDemo;

import java.sql.SQLSyntaxErrorException;

public class String_String1 {
    public static void main(String[] args) {
       
        //字符串的替换
        String str2="Hello World";
        System.out.println(str2.replace("Hello","HELLO"));

   
    }
}

九、字符串的转换

package com.company.StringDemo;

import java.sql.SQLSyntaxErrorException;

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

        //字符串的转换
        int va=200;
        String i=String.valueOf(va);
        System.out.println(i);    //200
        //Boolean 布尔型转换为String
        String b1 = String.valueOf(true);
        System.out.println(b1);  //true
        //char字符串转换为String
        String c=String.valueOf('c');
        System.out.println(c);   //c
        //char数组转换为String
        char[] chars={'a','b','c'};
        System.out.println(String.valueOf(chars));   //abc
        System.out.println(String.valueOf(chars,1,2));   //bc
    }
}

十、空串和NULL串

package com.company.StringDemo;

import java.sql.SQLSyntaxErrorException;

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

        //空串和NULL串
        String str3="";
        System.out.println(str3.length()==0);   //true
        System.out.println(str3.equals(""));    //true
        if(str3.length()==0){
            System.out.println("字符串为空");    //字符串为空
        }
        //判断字符串是否为null
        String str4=null;
        if(str4==null){
            System.out.println("字符串为null");  //字符串为null
        }
        //检查字符串既不是null也不是空
        if(str4!=null && str4.length()!=0){
            System.out.println("字符串既不是null也不是空");
        }else{
            System.out.println("字符串是空或者为null");   //字符串是空或者为null
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

敲代码敲到头发茂密

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

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

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

打赏作者

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

抵扣说明:

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

余额充值