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
        }


    }
}
相关推荐
zhanghaha13142 小时前
HTML系列教程:14_HTML 表单与输入框 <form>、<input> 零基础详解
java·前端·javascript
傻啦嘿哟7 小时前
某招聘平台爬虫:爬取招聘岗位数据,分析各城市薪资水平
开发语言·爬虫·python
2501_933670797 小时前
2026秋招量化分析岗技能栈:Python、SQL、统计建模、回测项目怎么准备
开发语言·python·sql
Dola_Zou7 小时前
工厂智能排产软件算法保护与按产线计费实战
算法·自动化·软件工程·软件加密
yxlalm7 小时前
Spring AI+RAG 01-项目背景与技术选型
java·人工智能·spring
程序员清风8 小时前
Java 后端高并发设计:线程池、限流、熔断与降级
java·数据库·oracle
李少兄8 小时前
JavaScript 数据类型完全指南
开发语言·javascript·ecmascript
Seoyoneh8 小时前
Agentic Workflow编排架构:云客服从“被动响应”迈向“主动执行”的技术实现
java·开发语言·架构
海南java第二人8 小时前
对外 API 如何保证幂等性?插入数据 / 上传表格场景全方案汇总
java·幂等性
啦啦啦啦啦zzzz8 小时前
Logger的组装(c++20)
c++·算法·c++20