Java字符串String

本篇介绍字符串处理类String一些常用API:连接、判空、截取、连接、包含字符串检测、返回字符索引等等。

package com.example.javatest;
/*
 *Author:W
 * String类
 */

public class MainTest {

    public static void main(String[] args)
    {
        System.out.println("====String字符串类====");

        String str1 = "Hello";
        String str2 = "World";

        System.out.println("str1的长度:"+str1.length());
        System.out.println("str1连接str2:"+str1.concat(str2));
        System.out.println("str1+str2:"+(str1+str2));
        System.out.println("字符串格式化:"+String.format("str1= %s",str1));
        System.out.println("返回指定索引处的 char 值:"+str1.charAt(2));
        System.out.println("字符串比较:"+str1.compareTo(str2));
        System.out.println("字符串连接:"+str1.concat(str2));
        System.out.println("字符串结尾符检测:"+str1.endsWith("d"));
        System.out.println("字符串开始符检测:"+str1.startsWith("H"));
        System.out.println("字符串相等检测:"+str1.equals(str2));
        System.out.println("字符串转byte数组:"+str1.getBytes());
        System.out.println("字符在字符串中第一次出现的索引:"+str1.indexOf("l"));
        System.out.println("字符在字符串中最后一次出现的索引:"+str1.lastIndexOf("l"));
        System.out.println("字符在字符串中替换:"+str1.replace("l","y"));
        System.out.println("在字符串中截取子字符串:"+str1.substring(2,3));
        System.out.println("字符串转小写:"+str1.toLowerCase());
        System.out.println("字符串转大写:"+str1.toUpperCase());
        System.out.println("在字符串是否包含字符串检测:"+str1.contains("el"));
        System.out.println("字符串空检查:"+str1.isEmpty());
    }
}

运行结果如下:

 

Guess you like

Origin blog.csdn.net/hppyw/article/details/119595504