当前位置: 首页 > article >正文

java常用字符串工具方法封装

Java常用的字符串工具方法有很多,以下是一些常见的封装:

  1. 判断字符串是否为空或null
public static boolean isNullOrEmpty(String str) {
    return str == null || str.trim().isEmpty();
}

  1. 判断字符串是否为数字
public static boolean isNumeric(String str) {
    if (isNullOrEmpty(str)) {
        return false;
    }
    try {
        Double.parseDouble(str);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

  1. 去除字符串中的空格
public static String trim(String str) {
    if (isNullOrEmpty(str)) {
        return str;
    }
    return str.trim();
}

  1. 拼接字符串
public static String join(String separator, String... strings) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < strings.length; i++) {
        if (i > 0) {
            sb.append(separator);
        }
        sb.append(strings[i]);
    }
    return sb.toString();
}

  1. 搜索指定字符串在原字符串中出现的次数
public static int countMatches(String str, String sub) {
    if (isNullOrEmpty(str) || isNullOrEmpty(sub)) {
        return 0;
    }
    int count = 0;
    int index = 0;
    while ((index = str.indexOf(sub, index)) != -1) {
        count++;
        index += sub.length();
    }
    return count;
}

  1. 判断字符串是否以指定前缀开头
public static boolean startsWith(String str, String prefix) {
    if (isNullOrEmpty(str) || isNullOrEmpty(prefix)) {
        return false;
    }
    return str.startsWith(prefix);
}

  1. 判断字符串是否以指定后缀结尾
public static boolean endsWith(String str, String suffix) {
    if (isNullOrEmpty(str) || isNullOrEmpty(suffix)) {
        return false;
    }
    return str.endsWith(suffix);
}

  1. 将字符串根据指定分隔符进行分割
public static String[] split(String str, String separator) {
    if (isNullOrEmpty(str)) {
        return new String[0];
    }
    return str.split(separator);
}

  1. 将字符串中的大小写进行转换
public static String toLowerCase(String str) {
    if (isNullOrEmpty(str)) {
        return str;
    }
    return str.toLowerCase();
}

public static String toUpperCase(String str) {
    if (isNullOrEmpty(str)) {
        return str;
    }
    return str.toUpperCase();
}

  1. 判断两个字符串是否相等,忽略大小写
public static boolean equalsIgnoreCase(String str1, String str2) {
    if (isNullOrEmpty(str1) || isNullOrEmpty(str2)) {
        return false;
    }
    return str1.equalsIgnoreCase(str2);
}


http://www.kler.cn/news/161173.html

相关文章:

  • 我们一起聊一聊JWT的那些事
  • 电商平台API接口文档演示案例,拼多多、淘宝、天猫、1688、京东、阿里巴巴、速卖通、LAZADA、虾皮APP商品详情API文档大全
  • CMakeLists获取文件夹——file指令
  • 形态学操作—细化
  • Python 日志(略讲)
  • Java程序员,你掌握了多线程吗?(文末送书)
  • 圣诞将至—C语言圣诞树代码来啦
  • VIR-SLAM代码分析3——VIR_VINS详解之estimator.cpp/.h
  • Wnmp本地搭建结合内网穿透实现远程访问本地Wnmp服务
  • 使用VS Code远程开发MENJA小游戏并通过内网穿透分享本地游戏到公网
  • NR重写console.log 增加时间信息
  • 在Linux上安装KVM虚拟机
  • 冒泡排序详解
  • Android 记录一些Framework开发的命令
  • Think in Java之多态
  • 2024年网络安全比赛--系统渗透测试(超详细)
  • vue.js el-table 动态单元格列合并
  • 数据库压力测试方法小结
  • TiDB专题---2、TiDB整体架构和应用场景
  • YashanDB练习SQL
  • CFLAGS、CXXFLAGS、FFLAGS、FCFLAGS、LDFLAGS、LD_LIBRARY_PATH区别
  • 高效的单行python脚本
  • C++ 指针进阶
  • Python-函数详解(局部、全局变量)
  • Springboot resource 下的excel
  • keep-alive 是 Vue 的一个内置组件,用于缓存其他组件的实例,以避免重复渲染和销毁,它可以在需要频繁切换的组件之间提供性能优化
  • Antd search input无中框
  • 2次MD5加密——用于分布式对话
  • 种下一棵栀子花
  • 先验概率和后验概率