java自定义封装StringUtils常用工具类
作者:Smile_Pride 发布时间:2022-09-01 05:11:13
标签:java,StringUtils
自定义封装StringUtils常用工具类,供大家参考,具体内容如下
package com.demo.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 字符串操作工具类
* @author dongyangyang
* @Date 2016/12/28 23:12
* @Version 1.0
*
*/
public class StringUtils {
/**
* 首字母变小写
* @param str
* @return
*/
public static String firstCharToLowerCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'A' && firstChar <= 'Z') {
char[] arr = str.toCharArray();
arr[0] += ('a' - 'A');
return new String(arr);
}
return str;
}
/**
* 首字母变大写
* @param str
* @return
*/
public static String firstCharToUpperCase(String str) {
char firstChar = str.charAt(0);
if (firstChar >= 'a' && firstChar <= 'z') {
char[] arr = str.toCharArray();
arr[0] -= ('a' - 'A');
return new String(arr);
}
return str;
}
/**
* 判断是否为空
* @param str
* @return
*/
public static boolean isEmpty(final String str) {
return (str == null) || (str.length() == 0);
}
/**
* 判断是否不为空
* @param str
* @return
*/
public static boolean isNotEmpty(final String str) {
return !isEmpty(str);
}
/**
* 判断是否空白
* @param str
* @return
*/
public static boolean isBlank(final String str) {
int strLen;
if ((str == null) || ((strLen = str.length()) == 0))
return true;
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* 判断是否不是空白
* @param str
* @return
*/
public static boolean isNotBlank(final String str) {
return !isBlank(str);
}
/**
* 判断多个字符串全部是否为空
* @param strings
* @return
*/
public static boolean isAllEmpty(String... strings) {
if (strings == null) {
return true;
}
for (String str : strings) {
if (isNotEmpty(str)) {
return false;
}
}
return true;
}
/**
* 判断多个字符串其中任意一个是否为空
* @param strings
* @return
*/
public static boolean isHasEmpty(String... strings) {
if (strings == null) {
return true;
}
for (String str : strings) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* checkValue为 null 或者为 "" 时返回 defaultValue
* @param checkValue
* @param defaultValue
* @return
*/
public static String isEmpty(String checkValue, String defaultValue) {
return isEmpty(checkValue) ? defaultValue : checkValue;
}
/**
* 字符串不为 null 而且不为 "" 并且等于other
* @param str
* @param other
* @return
*/
public static boolean isNotEmptyAndEquelsOther(String str, String other) {
if (isEmpty(str)) {
return false;
}
return str.equals(other);
}
/**
* 字符串不为 null 而且不为 "" 并且不等于other
* @param str
* @param other
* @return
*/
public static boolean isNotEmptyAndNotEquelsOther(String str, String... other) {
if (isEmpty(str)) {
return false;
}
for (int i = 0; i < other.length; i++) {
if (str.equals(other[i])) {
return false;
}
}
return true;
}
/**
* 字符串不等于other
* @param str
* @param other
* @return
*/
public static boolean isNotEquelsOther(String str, String... other) {
for (int i = 0; i < other.length; i++) {
if (other[i].equals(str)) {
return false;
}
}
return true;
}
/**
* 判断字符串不为空
* @param strings
* @return
*/
public static boolean isNotEmpty(String... strings) {
if (strings == null) {
return false;
}
for (String str : strings) {
if (str == null || "".equals(str.trim())) {
return false;
}
}
return true;
}
/**
* 比较字符相等
* @param value
* @param equals
* @return
*/
public static boolean equals(String value, String equals) {
if (isAllEmpty(value, equals)) {
return true;
}
return value.equals(equals);
}
/**
* 比较字符串不相等
* @param value
* @param equals
* @return
*/
public static boolean isNotEquals(String value, String equals) {
return !equals(value, equals);
}
public static String[] split(String content, String separatorChars) {
return splitWorker(content, separatorChars, -1, false);
}
public static String[] split(String str, String separatorChars, int max) {
return splitWorker(str, separatorChars, max, false);
}
public static final String[] EMPTY_STRING_ARRAY = new String[0];
private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return EMPTY_STRING_ARRAY;
}
List<String> list = new ArrayList<String>();
int sizePlus1 = 1;
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
if (separatorChars == null) {
while (i < len) {
if (Character.isWhitespace(str.charAt(i))) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else if (separatorChars.length() == 1) {
char sep = separatorChars.charAt(0);
while (i < len) {
if (str.charAt(i) == sep) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
} else {
while (i < len) {
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
}
lastMatch = false;
match = true;
i++;
}
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return (String[]) list.toArray(EMPTY_STRING_ARRAY);
}
/**
* 消除转义字符
* @param str
* @return
*/
public static String escapeXML(String str) {
if (str == null)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
switch (c) {
case '\u00FF':
case '\u0024':
break;
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\"':
sb.append(""");
break;
case '\'':
sb.append("'");
break;
default:
if (c >= '\u0000' && c <= '\u001F')
break;
if (c >= '\uE000' && c <= '\uF8FF')
break;
if (c >= '\uFFF0' && c <= '\uFFFF')
break;
sb.append(c);
break;
}
}
return sb.toString();
}
/**
* 将字符串 * 定模式的字符转换成map中对应的值
*
* @param s
* 需要转换的字符串
* @param map
* 转换所需的键值对集合
* @return 转换后的字符串
*/
public static String replace(String s, Map<String, Object> map) {
StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
int cursor = 0;
for (int start, end; (start = s.indexOf("${", cursor)) != -1 && (end = s.indexOf("}", start)) != -1;) {
ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end)));
cursor = end + 1;
}
ret.append(s.substring(cursor, s.length()));
return ret.toString();
}
public static String replace(String s, Object... objs) {
if (objs == null || objs.length == 0)
return s;
if (s.indexOf("{}") == -1)
return s;
StringBuilder ret = new StringBuilder((int) (s.length() * 1.5));
int cursor = 0;
int index = 0;
for (int start; (start = s.indexOf("{}", cursor)) != -1;) {
ret.append(s.substring(cursor, start));
if (index < objs.length)
ret.append(objs[index]);
else
ret.append("{}");
cursor = start + 2;
index++;
}
ret.append(s.substring(cursor, s.length()));
return ret.toString();
}
/**
* 字符串格式化工具,参数必须以{0}之类的样式标示出来.大括号中的数字从0开始。
*
* @param source
* 源字符串
* @param params
* 需要替换的参数列表,写入时会调用每个参数的toString().
* @return 替换完成的字符串。如果原始字符串为空或者参数为空那么将直接返回原始字符串。
*/
public static String replaceArgs(String source, Object... params) {
if (params == null || params.length == 0 || source == null || source.isEmpty()) {
return source;
}
StringBuilder buff = new StringBuilder(source);
StringBuilder temp = new StringBuilder();
int startIndex = 0;
int endIndex = 0;
String param = null;
for (int count = 0; count < params.length; count++) {
if (params[count] == null) {
param = null;
} else {
param = params[count].toString();
}
temp.delete(0, temp.length());
temp.append("{");
temp.append(count);
temp.append("}");
while (true) {
startIndex = buff.indexOf(temp.toString(), endIndex);
if (startIndex == -1) {
break;
}
endIndex = startIndex + temp.length();
buff.replace(startIndex, endIndex, param == null ? "" : param);
}
startIndex = 0;
endIndex = 0;
}
return buff.toString();
}
public static String substringBefore(final String s, final String separator) {
if (isEmpty(s) || separator == null) {
return s;
}
if (separator.isEmpty()) {
return "";
}
final int pos = s.indexOf(separator);
if (pos < 0) {
return s;
}
return s.substring(0, pos);
}
public static String substringBetween(final String str, final String open, final String close) {
if (str == null || open == null || close == null) {
return null;
}
final int start = str.indexOf(open);
if (start != -1) {
final int end = str.indexOf(close, start + open.length());
if (end != -1) {
return str.substring(start + open.length(), end);
}
}
return null;
}
public static String substringAfter(final String str, final String separator) {
if (isEmpty(str)) {
return str;
}
if (separator == null) {
return "";
}
final int pos = str.indexOf(separator);
if (pos == -1) {
return "";
}
return str.substring(pos + separator.length());
}
/**
* 转换为字节数组
* @param str
* @return
*/
public static String toString(byte[] bytes){
try {
return new String(bytes, "utf-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
/**
* 转换为字节数组
* @param str
* @return
*/
public static byte[] getBytes(String str){
if (str != null){
try {
return str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}else{
return null;
}
}
public static void main(String[] a){
String escapeXML = escapeXML("\\");
System.out.println(escapeXML);
}
}
来源:https://blog.csdn.net/qq_35712358/article/details/70254546
0
投稿
猜你喜欢
- 摘要:在spring boot中 MVC这部分也有默认自动配置,也就是说我们不用做任何配置,那么也是OK的,这个配置类就是 WebMvcAu
- 平时用到的库仓库名地址备注mavenCentralhttps://repo1.maven.org/maven2/
- 前言研究表明,Java堆中对象占据最大比重的就是字符串对象,所以弄清楚字符串知识很重要,本文主要重点聊聊字符串常量池。Java中的字符串常量
- 一、项目运行环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,
- Spring Cloud Gateway使用Spring Cloud Gateway是一个基于Spring Boot 2.x和Spring&
- 概念二叉搜索树又称二叉排序树,它或者是一棵空树,或者是具有以下性质的二叉树:1、若它的左子树不为空,则左子树上所有节点的值都小于根结点的值。
- 目录什么是异常?编译时还是运行时?“受检异常”究竟可不可取?我的观点什么是异常?要了解受检异常,首先要了解什么是异常。在Java中,异常是一
- 在Linux中创建一个新进程的唯一方法是使用fork()函数。fork()函数是Linux中一个非常重要的函数,和以往遇到的函数有一些区别,
- Java压缩文件与解压缩zip文件在日常的使用中经常会使用到像WinRAR或WinZIP这样的压缩文件,通过这些软件可以把一个很大的文件进行
- 背景在工作中,遇到这样的场景:有个es索引构建服务,需要从各个业务服务获取索引的信息,从而构建索引,业务服务都实现同一个接口&mda
- 集成引入依赖在项目的pom.xml中增加以下依赖<dependency> <groupId>org.s
- 本文实例为大家分享了opencv实现轮廓高斯滤波平滑的具体代码,供大家参考,具体内容如下一个小测试的题目:在图像上点选,找到与点选处相邻的颜
- 前言Java 中常规的集合工具,相比大家都熟练于胸,但是如果说有一个集合类你不一定知道或者说肯定没用过,你相不相信呢?今天跟大家介绍的就是&
- 本文实例讲述了C#4.0新特性的协变与逆变,有助于大家进一步掌握C#4.0程序设计。具体分析如下:一、C#3.0以前的协变与逆变如果你是第一
- 需求:字符串(字符串只有一位小数)转float进行运算, 将结果转成字符串(保留一位小数)直接上代码:float f1 = 0.1f;Str
- 本文实例讲述了Spring实战之协调作用域不同步的Bean操作。分享给大家供大家参考,具体如下:一 配置<?xml version=&
- 1.基本概念首先我们需要弄清楚几个概念:面向对象是什么、类是什么、对象又是什么?还是逐个来说1.1面向对象我们常说Java是面向对象的语言,
- 一.关于使用Mybatisplus自带的selectById和insert方法时的一些问题1.selectById的问题(1).表的主键列名
- 前言目前主流的锁有两种,一种是synchronized,另一种就是ReentrantLock,JDK优化到现在目前为止synchronize
- 1.@RequestMapping注解1.1@RequestMapping注解的功能从注解名称上我们可以看到,@RequestMapping