需求說明:
在Java中,如何刪除字符串中指定位置的字符?
解決方式:
在Java中並沒有提供一個直接刪除字符串中字符的方法,想要刪除字符需要自己封裝一個方法實現
方法一:通過從前往后循環每一個字符,如果不是要刪除的字符進行拼接處理。
package com.de.test; public class test { public static void main(String[] args) { String str = "How does Java remove the characters specified in the string?"; char delChar = 'a'; System.out.println(deleteString2(str,delChar)); } /** * 刪除方法一 * @param str * @param delChar * @return */ public static String deleteString0(String str, char delChar){ String delStr = ""; for (int i = 0; i < str.length(); i++) { if(str.charAt(i) != delChar){ delStr += str.charAt(i); } } return delStr; } /** * 刪除方法一 * @param str * @param delChar * @return */ public static String deleteString2(String str, char delChar) { StringBuffer stringBuffer = new StringBuffer(""); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) != delChar) { stringBuffer.append(str.charAt(i)); } } return stringBuffer.toString(); } }
運行結果:
How does Jv remove the chrcters specified in the string?
方法二:通過采用正則的方式和replaceAll函數,本種方法要注意特殊字符,例如正則中的 “.”字符,需要對特殊字符進行轉義
package com.de.test; public class test { public static void main(String[] args) { String str = "How does Java remove the characters specified in the string?"; char delChar = 'i'; System.out.println(deleteString1(str,delChar)); } /** *刪除方法二 * @return */ public static String deleteString1(String str, char delChar){ String delStr = ""; final String strTable = "|^$*+?.(){}\\"; String tmpRegex = "["; for (int i = 0; i < strTable.length(); i++) { if (strTable.charAt(i) == delChar) { tmpRegex += "//"; break; } } tmpRegex += delChar + "]"; delStr = str.replaceAll(tmpRegex, ""); return delStr; } }
運行結果:
How does Java remove the characters specfed n the strng?
方法三:把原字符串轉化為字符數組,然后原理與直接插入排序原理類似
package com.de.test; public class test { public static void main(String[] args) { String str = "How does Java remove the characters specified in the string?"; char delChar = 'e'; System.out.println(deleteString3(str,delChar)); } /** * 刪除方法三 * @param str * @param delChar * @return */ public static String deleteString3(String str, char delChar) { String delStr = ""; char[] Bytes = str.toCharArray(); int iSize = Bytes.length; for (int i = Bytes.length - 1; i >= 0; i--) { if (Bytes[i] == delChar) { for (int j = i; j < iSize - 1; j++) { Bytes[j] = Bytes[j + 1]; } iSize--; } } for (int i = 0; i < iSize; i++) { delStr += Bytes[i]; } return delStr; } }
運行結果:
How dos Java rmov th charactrs spcifid in th string?
方法四: 通過循環確定要刪除字符的位置索引,然后通過分割字符串的形式,將子字符串拼接,注意最后一段子字符串和源字符串中沒有要刪除字符的情況
package com.de.test; public class test { public static void main(String[] args) { String str = "How does Java remove the characters specified in the string?"; char delChar = 's'; System.out.println(deleteString4(str,delChar)); } /** * 刪除方法四 * @param str * @param delChar * @return */ public static String deleteString4(String str, char delChar) { String delStr = ""; int iIndex = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == delChar) { if (i > 0) { delStr += str.substring(iIndex, i); } iIndex = i + 1; } } if (iIndex <= str.length()) { delStr += str.substring(iIndex, str.length()); } return delStr; } }
運行結果:
How doe Java remove the character pecified in the tring?
總結:上面的方法參考了http://blog.csdn.net/li767517488/article/details/64919194