本文主要是介绍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公用类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!