String常用方法

String

方法
方法名参数返回值解释
contains()Stringboolean判断字符串是否包含参数String
endsWith()Stringboolean判断字符串是否以参数String结尾
startsWith()Stringboolean判断字符串是否以参数String开始
equals()Stringboolean判断两个字符串的值是否相等
equalsIgnoreCase()Stringboolean忽略大小写判断两个字符串的值是否相等
length()nullint返回字符串的长度
toLowerCase()nullString把字符串大写转换成小写
toUpperCase()nullString把字符串小写转换成大写
repeat()intString把字符串重复参数int
indexOfStringint获取当前参数String的索引位置,没有则返回-1
lastIndexOfStringint从字符串最后开始找参数String,返回该字符串索引位置
charAtintchar返回指定索引处的char值
substringintString获取从索引****开始到结尾的子串,并返回
substringint,intString获取两个索引之间的子串,并返回
lastIndexOfStringint返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
replaceCharSequence,CharSequenceString参数1替换参数2
linesnullStream获取行
countnulllong计数
concatStringString连接字符串类似于 " “+” ";
trimnullString清楚首尾连续的空格
stripnullString清楚首尾连续的空格
stripLeadingnullString清楚首部连续空格
stripTrailingnullString清楚尾部连续空格
isEmptynullString判断内容是不是空的!
isBlanknullString先移除空格 再判定是否有字符!
import java.util.Calendar;
import java.util.UUID;

/**
 * @author Mxhlin
 * @Email fuhua277@163.com
 * @Date 2022/09/13/15:03
 * @Version
 * @Description String基本语法
 */
public class Str01 {
    public static void main(String[] args) {
        String str = "hello java java 19 domo.";
        // 判断
        System.out.println(str.contains("java"));// 是否包含java
        System.out.println("abc.jpg".endsWith(".jpg"));// 是否以.jpg结尾
        System.out.println("Hello.jpg".startsWith("Hello"));// 是否以Hello开始
        System.out.println("hello".equals("hello"));// 判断是否相等
        System.out.println("hello".equalsIgnoreCase("Hello"));// 忽略大小写是否相等
        System.out.println("hello".length()>3);// 字符串的长度是否大于3

        // 转换
        System.out.println("hello JAVA".toLowerCase());// 把大写转换成小写
        System.out.println("hello JAVA".toUpperCase());// 把小写转换成大写
        System.out.println("*".repeat(39));// 重复次数

        // 索引
        System.out.println(str.indexOf("java"));// 获取字符在字符串的索引位置
        System.out.println(str.lastIndexOf("java"));// 从字符串最后开始找java,返回该字符串索引位置
        System.out.println(str.indexOf("go"));// -1
        System.out.println("星期"+"日一二三四五六".charAt(Calendar.getInstance().get(Calendar.DAY_OF_WEEK)-1));

        // 截取字符串
        System.out.println("hello".substring(2));// 获取从索引2开始到结尾的子串,并返回
        System.out.println("hello".substring(2,4));// 获取两个索引之间的子串,并返回
        String pic = "A:\\user\\uploads\\20220912.jpg";
        System.out.println(pic.substring(pic.lastIndexOf(".")));// .jpg

        // 获取文件名
        System.out.println(pic.substring(pic.lastIndexOf("\\")+1));// 20220912.jpg


        System.out.println(pic.substring(0,pic.lastIndexOf("\\")));

        // 修改文件名
        String s = UUID.randomUUID().toString().toUpperCase();// 获取大写的UUID
        System.out.println(s);
        System.out.println(pic.substring(0,pic.lastIndexOf("\\")+1)+s+pic.substring(pic.lastIndexOf(".")));

        // 替换,删除
        System.out.println("java hello 19 go".replace("java","python"));// 替换
        System.out.println("java hello 19 go".replace(" ",""));// 删除

        System.out.println("java123go12hello324".replace("\\d",""));// 删除字符串整数类型

        // 字符串倒叙
        String ss = "java123";
        System.out.println(ss);
        System.out.println(new StringBuffer(ss).reverse());
        String t = "";
        for (int i = 0; i < ss.length(); i++) {
            t = ss.charAt(i) + t;
        }
        System.out.println(t);
    }
}

image-20220913194235267

/**
 * @author Mxhlin
 * @Email fuhua277@163.com
 * @Date 2022/09/13/19:25
 * @Version
 * @Description
 */
public class Str03 {
    public static void main(String[] args) {
        String s1 = "       jdbc     java          out    ";
        // concat()链接  参数为字符串,返回值字符串
        // repeat()重复  参数为int,返回值为String
        System.out.println("Hello".concat("\s".repeat(30)).concat("java"));
        System.out.println("Hello" + "\s".repeat(30) + ("java"));

        // 清楚首尾连续的空格
        System.out.println(s1.trim());
        System.out.println(s1.strip());

        System.out.println(s1.stripLeading());// 清楚首部连续空格
        System.out.println(s1.stripTrailing());// 清楚尾部连续空格

        // 清楚所有空格
        System.out.println(s1.replace(" ", ""));

        System.out.println("------------------------------------");
        System.out.println("".isBlank());
        System.out.println("".isEmpty());

        System.out.println(" ".isEmpty());// 判断内容是不是空的!
        System.out.println(" ".isBlank());// 先移除空格 再判定是否有字符!
        System.out.println(" ".trim().length() == 0 ? "空字符串" : "正确");
        System.out.println(" ".isBlank() ? "空字符串" : "正确");
        System.out.println("*".repeat(60));
        System.out.println("D:\\peixun\\java\\Lx\\src\\com\\Mxhlin\\String\\Str03.java\njava\ndlaj".lines().count());// 字符串的长度
    }
}

image-20220913194147361

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值