加载中...

JavaSE常用类之String

String类

String类常用方法:

修饰符和返回值的类型 方法名 解释
char charAt() 获取某个位置的字符
String concat() 字符串的拼接。一般字符串拼接直接相加就好了
boolean contains() 判断原字符串是否含有xxx字符串,常用于子串的判断
boolean endsWith() 判断原字符串是否以xxx字符串结尾
boolean startsWith() 判断原字符串是否以xxx字符串开头
boolean equals() 判断两边字符串内容是否相同;==判断地址是否相同
boolean equalsIgnoreCase() 忽略大小写判断两边字符串的内容是否一样
int indexOf() 计算给出字符串第一个出现的位置
int LastindexOf() 计算给出字符串最后一个出现的位置
int length() 计算字符串的长度
String replace() 字符串内容的替换
String[] split() 字符串切割,最终结果是一个字符串数组
String substring() 字符串截取,左闭右开:[ )
String trim() 去掉字符串左右两边的空格,中间的不行
static String valueOf() 官方:基本数据类型转为字符串操作;直接:变量 + ""

字符串是一个不可变的类型(final类),几乎所有的字符串操作都会返回一个新字符串而不是在原有基础上进行修改。

代码:

package Stringl;

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

        String s2 = new String("我愿意");//String 对象创建
        String s3="我愿意";        // String 直接创建
        String s4=s3;          //相同引用

        //注意:几乎所有的字符串操作都会返回一个新字符串而不是在原有基础上进行修改

        String s="我的名字叫";
        s.concat("张三");     //在字符串s上拼接
        //字符串是不可变的数据类型
        System.out.println(s);   //我的名字叫
        //字符串操作都会返回一个新字符串
        String s1=s.concat("张三");
        System.out.println(s1);   //我的名字叫张三

        String str1="张三非常喜欢";
        str1.replace("张三","李四");
        System.out.println(str1);  //不变   张三非常喜欢
        String str2=str1.replace("张三","李四");
        System.out.println(str2);   //返回新的字符串  李四非常喜欢

        System.out.println(s.charAt(1));  //获取第0个位置的字符 返回:的
        //判断字符串中是否有xxx字符
        System.out.println(str1.contains("张三"));   //true
        System.out.println(str1.contains("李四"));    //false

        String cd="一起参加算法比赛";
        //返回长度
        System.out.println(cd.length());   // 8
        //判断是否以xxx为结尾
        System.out.println(cd.endsWith("比赛"));      //true
        //判断是否以xxx开头
        System.out.println(cd.startsWith("参加"));     //false

        String e="asl";
        String e1="asl";
        // equals字符串内容是否相同
        System.out.println(e.equals(e1));          //true

        //字符串切割,最终结果是一个字符串数组
        String d="江西-南昌-青山湖-NCU";
        String[] dd=d.split("-");
        System.out.println(dd[0]);  //江西
        System.out.println(dd[1]);   //南昌
        System.out.println(dd[2]);   //青山湖
        System.out.println(dd[3]);    //NCU

        String str3="希望每天的很开心!";
        System.out.println(str3.substring(2,5));   //每天的   返回2-4(不包括5

        String h="嘻嘻我习我是你的我嘿嘿我的";
        //计算给出字符串第一个出现的位置
        System.out.println(h.indexOf("我"));   //2
        //计算给出字符串最后一个出现的位置
        System.out.println(h.lastIndexOf("我"));   //11

        //去掉字符串左右两边的空格,中间的不行
        String str4="  开  心   的   ";
        System.out.println(str4);  //  开  心   的
        System.out.println(str4.trim());    //开  心   的

        //基本数据类型转为字符串操作
        int f=100;
        System.out.println(String.valueOf(100));
        System.out.println(f+"");
        //是否为空
        String k="woe";
        System.out.println(k.isEmpty());   //flase

        String a="c"+1+2;
//        String a1='c'+1+2;
        String a2=1+2+"c";
        System.out.println(a);    //c12
        System.out.println(a2);    //3c
    }
}

posted @ 2022-04-08 11:43  别捏小祎脸了  阅读(30)  评论(0)    收藏  举报