您好,登录后才能下订单哦!
本篇内容主要讲解“JAVA之String中删除指定字符方式的方法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“JAVA之String中删除指定字符方式的方法有哪些”吧!
通过循环从前往后遍历,如果不是要删除的字符则加到处理后的字符串中
代码如下:
public String deleteCharString0(String sourceString, char chElemData) { String deleteString = ""; for (int i = 0; i < sourceString.length(); i++) { if (sourceString.charAt(i) != chElemData) { deleteString += sourceString.charAt(i); } } return deleteString; }
通过循环确定要删除字符的位置索引,然后通过分割字符串的形式,将子字符串拼接,注意最后一段子字符串和源字符串中没有要删除字符的情况
代码如下:
public String deleteCharString1(String sourceString, char chElemData) { String deleteString = ""; int iIndex = 0; for (int i = 0; i < sourceString.length(); i++) { if (sourceString.charAt(i) == chElemData) { if (i > 0) { deleteString += sourceString.substring(iIndex, i); } iIndex = i + 1; } } if (iIndex <= sourceString.length()) { deleteString += sourceString.substring(iIndex, sourceString.length()); } return deleteString; }
原理同上,只不过查找要删除字符位置采用String类中的函数执行,效率不如上面的高
代码如下:
public String deleteCharString2(String sourceString, char chElemData) { String deleteString = ""; int iIndex = 0; int tmpCount = 0; do { tmpCount = sourceString.indexOf(chElemData, iIndex); if (tmpCount > 0) { deleteString += sourceString.substring(iIndex, tmpCount); } if (tmpCount != -1) { iIndex = tmpCount + 1; } } while (tmpCount != -1); if (iIndex <= sourceString.length()) { deleteString += sourceString.substring(iIndex, sourceString.length()); } return deleteString; }
原理与上方基本一致,只不过这次采用倒序方式,这里的坑就更多了,一定要注意索引的取值范围和是否合法
代码如下:
public String deleteCharString3(String sourceString, char chElemData) { String deleteString = ""; int iIndex = sourceString.length(); int tmpCount = 0; do { tmpCount = sourceString.lastIndexOf(chElemData, iIndex - 1); if (tmpCount < sourceString.length() && tmpCount >= 0) { deleteString = sourceString.substring(tmpCount + 1, iIndex) + deleteString; } if (tmpCount != -1) { iIndex = tmpCount; } } while (tmpCount != -1); if (iIndex >= 0) { deleteString = sourceString.substring(0, iIndex) + deleteString; } return deleteString; }
通过采用正则的方式和replaceAll函数,本种方法要注意特殊字符,例如正则中的 “.”字符,需要对特殊字符进行转义
代码如下:
public String deleteCharString4(String sourceString, char chElemData) { String deleteString = ""; final String strTable = "|^$*+?.(){}\\"; String tmpRegex = "["; for (int i = 0; i < strTable.length(); i++) { if (strTable.charAt(i) == chElemData) { tmpRegex += "\\"; break; } } tmpRegex += chElemData + "]"; deleteString = sourceString.replaceAll(tmpRegex, ""); return deleteString; }
采用正则的方式将字符串分割成几个子字符串,再将子字符串进行拼接
代码如下:
public String deleteCharString5(String sourceString, char chElemData) { String deleteString = ""; final String strTable = "|^$*+?.(){}\\"; String tmpRegex = "["; for (int i = 0; i < strTable.length(); i++) { if (strTable.charAt(i) == chElemData) { tmpRegex += "\\"; break; } } tmpRegex += chElemData + "]"; String[] tmpStringArray = sourceString.split(tmpRegex); for (int i = 0; i < tmpStringArray.length; i++) { deleteString += tmpStringArray[i]; } return deleteString; }
将字符编程可读序列,在通过 String 类中的方法替换
代码如下:
public String deleteCharString6(String sourceString, char chElemData) { String tmpString = ""; tmpString += chElemData; tmpString.subSequence(0, 0); String deleteString = ""; deleteString = sourceString.replace(tmpString, deleteString.subSequence(0, 0)); return deleteString; }
把原字符串转化为字符数组,然后原理与直接插入排序原理类似
代码如下:
public String deleteCharString7(String sourceString, char chElemData) { String deleteString = ""; char[] Bytes = sourceString.toCharArray(); int iSize = Bytes.length; for (int i = Bytes.length - 1; i >= 0; i--) { if (Bytes[i] == chElemData) { for (int j = i; j < iSize - 1; j++) { Bytes[j] = Bytes[j + 1]; } iSize--; } } for (int i = 0; i < iSize; i++) { deleteString += Bytes[i]; } return deleteString; }
原理与 第一种方法 类似,本次采用 stringBuffer 类中的 append 方法进行操作,我认为效率应该高于第一种。
public String deleteCharString8(String sourceString, char chElemData) { StringBuffer stringBuffer = new StringBuffer(""); for (int i = 0; i < sourceString.length(); i++) { if (sourceString.charAt(i) != chElemData) { stringBuffer.append(sourceString.charAt(i)); } } return stringBuffer.toString(); }
采用 stringBuffer 类中的 replace and indexOf 方法(_ 故意凑方法)
代码如下:
public String deleteCharString9(String sourceString, char chElemData) { String tmpString = ""; tmpString += chElemData; StringBuffer stringBuffer = new StringBuffer(sourceString); int iFlag = -1; do { iFlag = stringBuffer.indexOf(tmpString); if (iFlag != -1) { stringBuffer = stringBuffer.replace(iFlag, iFlag + 1, ""); } } while (iFlag != -1); return stringBuffer.toString(); }
采用 stringBuffer 类中的 deleteCharAt 和 indexOf 直接删除
public String deleteCharString10(String sourceString, char chElemData) { String tmpString = ""; tmpString += chElemData; StringBuffer stringBuffer = new StringBuffer(sourceString); int iFlag = -1; do { iFlag = stringBuffer.indexOf(tmpString); if (iFlag != -1) { stringBuffer.deleteCharAt(iFlag); } } while (iFlag != -1); return stringBuffer.toString(); }
程序运行截图:
到此,相信大家对“JAVA之String中删除指定字符方式的方法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。