Java筑基—String类


这里写目录标题

一、字符串的拼接

Java语言允许使用 + 号拼接两个字符串

注意:当将一个字符串与一个非字符串的值进行拼接时,后者被转换成字符串。

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 );
    }
}
java 复制代码
kobe
age is 18

二、获取字符串长度

获取字符串长度:length()

获取字符串的长度

返回int类型

java 复制代码
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():所有字母大写的字符串

字符串的全部转换

java 复制代码
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);

    }
}
java 复制代码
helloworld
HELLOWORLD

四、去除前后空白字符

trim():删除字符串2端的空白字符返回一个新的字符串。

空白字符:" ",、\t,\f,\n,\r

java 复制代码
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
    }
}
java 复制代码
 this is string 1
this is string1
16
14
-----------------字符串去除空白字符---------
 this is string 	1
this is string1
18
14

五、比较字符串是否相等

equals(s1):将字符串与s1进行比较

忽略大小写比较:s1.equalsIgnoreCase(s2)

不能使用"=="运算符对两个字符串进行相等比较

==:只能确定两个字符串是否指向同一个对象

不会比较字符串的内容是否相等

java 复制代码
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对应的内容是否相同

    }
}

六、比较字符串是否包含

java 复制代码
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

    }
}

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

java 复制代码
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);
    }
}

八、字符串的替换

java 复制代码
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"));

   
    }
}

九、字符串的转换

java 复制代码
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串

java 复制代码
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
        }


    }
}
相关推荐
珊珊而川8 分钟前
SAUP 算法
算法
.YM.Z11 分钟前
数据结构——链表(二)
数据结构·链表
珹洺14 分钟前
Java-Spring入门指南(二十七)Android Studio 第一个项目搭建与手机页面模拟器运行
java·spring·android studio
寂静山林28 分钟前
UVa 1471 Defense Lines
算法
程序猿DD36 分钟前
Java 25 中的 6 个新特性解读
java·后端
稻草猫.39 分钟前
文件 IO
java·笔记·后端·java-ee·idea
laopeng30140 分钟前
基于Spring AI Deep Researcher Agent
java·人工智能·spring
纵有疾風起1 小时前
C++模版:模板初阶及STL简介
开发语言·c++·经验分享·开源
QT 小鲜肉1 小时前
【个人成长笔记】Qt Creator快捷键终极指南:从入门到精通
开发语言·c++·笔记·qt·学习·学习方法
子豪-中国机器人1 小时前
《C++ STL 基础入门》教案
java·开发语言