Java基础(⼀)字符串之如何删除指定的字符需求说明:
  在Java中,如何删除字符串中指定位置的字符?
解决⽅式:
  在Java中并没有提供⼀个直接删除字符串中字符的⽅法,想要删除字符需要⾃⼰封装⼀个⽅法实现
⽅法⼀:通过从前往后循环每⼀个字符,如果不是要删除的字符进⾏拼接处理。
package st;
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));
}
}
String();
}
}
运⾏结果:
string字符串转化数组
How does Jv remove the chrcters specified in the string?
⽅法⼆:通过采⽤正则的⽅式和replaceAll函数,本种⽅法要注意特殊字符,例如正则中的 “.”字符,需要对特殊字符进⾏转义package st;
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 = placeAll(tmpRegex, "");
return delStr;
}
}
运⾏结果:
How does Java remove the characters specfed n the strng?
⽅法三:把原字符串转化为字符数组,然后原理与直接插⼊排序原理类似
package st;
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 = CharArray();
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 st;
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?

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。