JavaUtilString公用类

2024-08-20 18:58
文章标签 java 公用 utilstring

本文主要是介绍JavaUtilString公用类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

自己以往项目中收集整理的一些String操作:
package platform.commons.utils;import java.util.ArrayList;public class UtilString
{public String strSuper;protected String lowcaseSuper;public UtilString(){this.strSuper = new String();this.lowcaseSuper = this.strSuper;}public UtilString(String strInit) {this.strSuper = new String(strInit);this.lowcaseSuper = strInit.toLowerCase();}public ArrayList<String> split(String strSign) {int begin = 0;int end = 0;ArrayList vecList = new ArrayList();if (strSign == ""){for (int i = 0; i < this.strSuper.length(); i++) {vecList.add(this.strSuper.substring(i, i + 1));}return vecList;}end = this.strSuper.indexOf(strSign);if (end == -1) {vecList.add(this.strSuper);return vecList;}do {vecList.add(this.strSuper.substring(begin, end));begin = end + strSign.length();end = this.strSuper.indexOf(strSign, begin);}while (end >= 0);vecList.add(this.strSuper.substring(begin, this.strSuper.length()));return vecList;}public int matchOf(String strBegin){return matchOf(strBegin, false);}public int matchOf(String strBegin, boolean ignoreCase) {int pos = -1;if (ignoreCase)pos = this.lowcaseSuper.indexOf(strBegin.toLowerCase());else {pos = this.strSuper.indexOf(strBegin);}if (pos < 0) {return pos;}return pos + strBegin.length();}public int lastMatchOf(String strEnd) {return lastMatchOf(strEnd, false);}public int lastMatchOf(String strEnd, boolean ignoreCase) {if (ignoreCase) {return this.lowcaseSuper.lastIndexOf(strEnd.toLowerCase());}return this.strSuper.lastIndexOf(strEnd);}public String matchValue(String strBegin, String strEnd){return matchValue(strBegin, strEnd, false);}public String matchValue(String strBegin, String strEnd, boolean ignoreCase) {String strResult = new String();int posBegin = matchOf(strBegin, ignoreCase);int posEnd = lastMatchOf(strEnd, ignoreCase);if ((posEnd > posBegin) && (posBegin != -1)) {strResult = this.strSuper.substring(posBegin, posEnd);}return strResult;}public String replace(String strOld, String strNew) {return replace(strOld, strNew, false);}public String replace(String strOld, String strNew, boolean ignoreCase){String strResult = new String();String strReplace;String strCore;String strReplace;if (ignoreCase) {String strCore = this.lowcaseSuper;strReplace = strOld.toLowerCase();} else {strCore = this.strSuper;strReplace = strOld;}int posBegin = 0;int posEnd = strCore.indexOf(strReplace);if (posEnd < 0) {return this.strSuper;}do{strResult = strResult + this.strSuper.substring(posBegin, posEnd);strResult = strResult + strNew;posBegin = posEnd + strReplace.length();posEnd = strCore.indexOf(strReplace, posBegin);}while (posEnd >= 0);strResult = strResult + this.strSuper.substring(posBegin);return strResult.toString();}public String replace(String strBegin, String strEnd, String strNew) {return replace(strBegin, strEnd, strNew, false);}public String replace(String strBegin, String strEnd, String strNew, boolean ignoreCase){int posBegin = matchOf(strBegin, ignoreCase);int posEnd = lastMatchOf(strEnd, ignoreCase);String strResult = new String();if ((posEnd >= posBegin) && (posBegin != -1)) {strResult = this.strSuper.substring(0, posBegin);strResult = strResult + strNew;strResult = strResult + this.strSuper.substring(posEnd);return strResult;}return this.strSuper;}public boolean startsWith(String strBegin){return startsWith(strBegin, false);}public boolean startsWith(String strBegin, boolean ignoreCase){if (ignoreCase) {return this.lowcaseSuper.startsWith(strBegin.toLowerCase());}return this.strSuper.startsWith(strBegin);}public boolean endsWith(String strEnd){return endsWith(strEnd, false);}public boolean endsWith(String strEnd, boolean ignoreCase) {if (ignoreCase) {return this.lowcaseSuper.endsWith(strEnd.toLowerCase());}return this.strSuper.endsWith(strEnd);}public static String cutString(String str, int length){if (str == null) {return "";}if ((str.length() > length) && (length > 2)) {return str.substring(0, length) + ".";}return str;}
}

这篇关于JavaUtilString公用类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090911

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一