需求说明:
在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
















