Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

String常用方法

 Share


NedK7

Recommended Posts

 

String

方法

方法名 参数 返回值 解释
contains() String boolean 判断字符串是否包含参数String
endsWith() String boolean 判断字符串是否以参数String结尾
startsWith() String boolean 判断字符串是否以参数String开始
equals() String boolean 判断两个字符串的值是否相等
equalsIgnoreCase() String boolean 忽略大小写判断两个字符串的值是否相等
length() null int 返回字符串的长度
toLowerCase() null String 把字符串大写转换成小写
toUpperCase() null String 把字符串小写转换成大写
repeat() int String 把字符串重复参数int
indexOf String int 获取当前参数String的索引位置,没有则返回-1
lastIndexOf String int 从字符串最后开始找参数String,返回该字符串索引位置
charAt int char 返回指定索引处的char值
substring int String 获取从索引****开始到结尾的子串,并返回
substring int,int String 获取两个索引之间的子串,并返回
lastIndexOf String int 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
replace CharSequence,CharSequence String 参数1替换参数2
lines null Stream 获取行
count null long 计数
concat String String 连接字符串类似于 " "+" ";
trim null String 清楚首尾连续的空格
strip null String 清楚首尾连续的空格
stripLeading null String 清楚首部连续空格
stripTrailing null String 清楚尾部连续空格
isEmpty null String 判断内容是不是空的!
isBlank null String 先移除空格 再判定是否有字符!
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);
    }
}

 

/**
 * @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());// 字符串的长度
    }
}
Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...