java===java基础学习(4)---字符串操作

时间:2023-03-09 05:37:59
java===java基础学习(4)---字符串操作

java中的字符串操作和python中的大致相同,需要熟悉的就是具体操作形式。

关于具体api的使用,详见:java===字符串常用API介绍(转)

package testbotoo;

public class shuzhileixingzhuanhuan {

    public static void main(String[] args){

        String greeting = "hello word";

        //string 类的substring 方法可以实现字符串的提取,提取一个子串。
String s = greeting.substring(3,8);
System.out.println(s); //结果为 lo wo //字符串的拼接
int a = 13;
String c = "hahhah我的";
String d = a +c;
System.out.println(d);
//不可变字符串 //比如,将greeting的更改为 help!
greeting = greeting.substring(0,3)+"p!";
System.out.println(greeting); //help! int n = greeting.length();
System.out.println(n);
int cpCount = greeting.codePointCount(0,n);
System.out.println(cpCount); StringBuilder builder = new StringBuilder();
builder.append("sdasada");
builder.append(123); System.out.println(int length());
String completedString = builder.toString(); //调用toString方法,可以得到一个string对象
System.out.println(completedString);
} }

检查字符串是否相等
        表达式:s.equals(t)
        如果相等,返回True.如果不想等,返回False
        **尤其注意的是,不能使用==运算符检测两个字符串是否相等!
        
        空串与Null串
        *空串""是长度为0的字符串
        *Null串,目前没有任何对象与该变量关联。
        可以使用以下代码检查一个字符串是否为空
        if (str.length() == 0)
        或者
        if (str.equals(""))
        可以使用以下代码检查是否为null
        if (str == null)