跳转到帖子
  • 游客您好,欢迎来到黑客世界论坛!您可以在这里进行注册。

    赤队小组-代号1949(原CHT攻防小组)在这个瞬息万变的网络时代,我们保持初心,创造最好的社区来共同交流网络技术。您可以在论坛获取黑客攻防技巧与知识,您也可以加入我们的Telegram交流群 共同实时探讨交流。论坛禁止各种广告,请注册用户查看我们的使用与隐私策略,谢谢您的配合。小组成员可以获取论坛隐藏内容!

    TheHackerWorld官方

String常用方法


NedK7

推荐的帖子

 

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());// 字符串的长度
    }
}
链接帖子
意见的链接
分享到其他网站

黑客攻防讨论组

黑客攻防讨论组

    You don't have permission to chat.
    • 最近浏览   0位会员

      • 没有会员查看此页面。
    ×
    ×
    • 创建新的...