String删除字符

String类没有提供删除的类方法,但删除字符又属于常见需求。

String st = "abcd";

一、删除特定字符

这个有点复杂。直接转载了,原文见 https://blog.csdn.net/linzhiqiang0316/article/details/90440696

第一种方法

通过循环从前往后遍历,如果不是要删除的字符则加到处理后的字符串中,代码如下:

1     public String deleteCharString0(String sourceString, char chElemData) {
2         String deleteString = "";
3         for (int i = 0; i < sourceString.length(); i++) {
4             if (sourceString.charAt(i) != chElemData) {
5                 deleteString += sourceString.charAt(i);
6             }
7         }
8         return deleteString;
9     }

第二种方法

通过循环确定要删除字符的位置索引,然后通过分割字符串的形式,将子字符串拼接,注意最后一段子字符串和源字符串中没有要删除字符的情况,代码如下:

 1     public String deleteCharString1(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         int iIndex = 0;
 4         for (int i = 0; i < sourceString.length(); i++) {
 5             if (sourceString.charAt(i) == chElemData) {
 6                 if (i > 0) {
 7                     deleteString += sourceString.substring(iIndex, i);
 8                 }
 9                 iIndex = i + 1;
10             }
11         }
12         if (iIndex <= sourceString.length()) {
13             deleteString += sourceString.substring(iIndex, sourceString.length());
14         }
15         return deleteString;
16     }

第三种方法

原理同上,只不过查找要删除字符位置采用String类中的函数执行,效率不如上面的高,代码如下:

 1     public String deleteCharString2(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         int iIndex = 0;
 4         int tmpCount = 0;
 5         do {
 6             tmpCount = sourceString.indexOf(chElemData, iIndex);
 7             if (tmpCount > 0) {
 8                 deleteString += sourceString.substring(iIndex, tmpCount);
 9             }
10             if (tmpCount != -1) {
11                 iIndex = tmpCount + 1;
12             }
13         } while (tmpCount != -1);
14         if (iIndex <= sourceString.length()) {
15             deleteString += sourceString.substring(iIndex, sourceString.length());
16         }
17         return deleteString;
18     }

 

第四种方法

原理与上方基本一致,只不过这次采用倒序方式,这里的坑就更多了,一定要注意索引的取值范围和是否合法,代码如下:

 1     public String deleteCharString3(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         int iIndex = sourceString.length();
 4         int tmpCount = 0;
 5         do {
 6             tmpCount = sourceString.lastIndexOf(chElemData, iIndex - 1);
 7             if (tmpCount < sourceString.length() && tmpCount >= 0) {
 8                 deleteString = sourceString.substring(tmpCount + 1, iIndex) + deleteString;
 9             }
10             if (tmpCount != -1) {
11                 iIndex = tmpCount;
12             }
13         } while (tmpCount != -1);
14         if (iIndex >= 0) {
15             deleteString = sourceString.substring(0, iIndex) + deleteString;
16         }
17  
18         return deleteString;
19     }

 

第五种方法

通过采用正则的方式和replaceAll函数,本种方法要注意特殊字符,例如正则中的 “.”字符,需要对特殊字符进行转义,代码如下:

 1     public String deleteCharString4(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         final String strTable = "|^$*+?.(){}\\";
 4         String tmpRegex = "["; 
 5         for (int i = 0; i < strTable.length(); i++) {
 6             if (strTable.charAt(i) == chElemData) {
 7                 tmpRegex += "\\";
 8                 break;
 9             }
10         }
11         tmpRegex += chElemData + "]";
12         deleteString = sourceString.replaceAll(tmpRegex, "");
13         return deleteString;
14     }

 

第六种方法

采用正则的方式将字符串分割成几个子字符串,再将子字符串进行拼接,代码如下:

 1     public String deleteCharString5(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         final String strTable = "|^$*+?.(){}\\";
 4         String tmpRegex = "["; 
 5         for (int i = 0; i < strTable.length(); i++) {
 6             if (strTable.charAt(i) == chElemData) {
 7                 tmpRegex += "\\";
 8                 break;
 9             }
10         }
11         tmpRegex += chElemData + "]";
12         String[] tmpStringArray = sourceString.split(tmpRegex);
13         for (int i = 0; i < tmpStringArray.length; i++) {
14             deleteString += tmpStringArray[i];
15         }
16         return deleteString;
17     }

 

第七种方法

将字符编程可读序列,在通过 String 类中的方法替换,代码如下:

1     public String deleteCharString6(String sourceString, char chElemData) {
2         String tmpString = "";
3         tmpString += chElemData;
4         tmpString.subSequence(0, 0);
5         String deleteString = "";
6         deleteString = sourceString.replace(tmpString, deleteString.subSequence(0, 0));
7         return deleteString;
8     }

 

第八种方法

把原字符串转化为字符数组,然后原理与直接插入排序原理类似,代码如下:

 1     public String deleteCharString7(String sourceString, char chElemData) {
 2         String deleteString = "";
 3         char[] Bytes = sourceString.toCharArray();
 4         int iSize = Bytes.length;
 5         for (int i = Bytes.length - 1; i >= 0; i--) {
 6             if (Bytes[i] == chElemData) {
 7                 for (int j = i; j < iSize - 1; j++) {
 8                     Bytes[j] = Bytes[j + 1];
 9                 }
10                 iSize--;
11             }
12         }
13         for (int i = 0; i < iSize; i++) {
14             deleteString += Bytes[i];
15         }
16         return deleteString;
17     }

 

第九种方法

原理与 第一种方法 类似,本次采用 stringBuffer 类中的 append 方法进行操作,我认为效率应该高于第一种。

1     public String deleteCharString8(String sourceString, char chElemData) {
2         StringBuffer stringBuffer = new StringBuffer("");
3         for (int i = 0; i < sourceString.length(); i++) {
4             if (sourceString.charAt(i) != chElemData) {
5                 stringBuffer.append(sourceString.charAt(i));
6             }
7         }
8         return stringBuffer.toString();
9     }

 

第十种方法

采用 stringBuffer 类中的 replace and indexOf 方法(_ 故意凑方法),代码如下:

 1     public String deleteCharString9(String sourceString, char chElemData) {
 2         String tmpString = "";
 3         tmpString += chElemData;
 4         StringBuffer stringBuffer = new StringBuffer(sourceString);
 5         int iFlag = -1;
 6         do {
 7             iFlag = stringBuffer.indexOf(tmpString);
 8             if (iFlag != -1) {
 9                 stringBuffer = stringBuffer.replace(iFlag, iFlag + 1, "");
10             }
11         } while (iFlag != -1);
12         return stringBuffer.toString();
13     }

 

第十一种方法

采用 stringBuffer 类中的 deleteCharAt 和 indexOf 直接删除

 1     public String deleteCharString10(String sourceString, char chElemData) {
 2         String tmpString = "";
 3         tmpString += chElemData;
 4         StringBuffer stringBuffer = new StringBuffer(sourceString);
 5         int iFlag = -1;
 6         do {
 7             iFlag = stringBuffer.indexOf(tmpString);
 8             if (iFlag != -1) {
 9                 stringBuffer.deleteCharAt(iFlag);
10             }
11         } while (iFlag != -1);
12         return stringBuffer.toString();
13     }

 

二、删除指定位置字符

这个简单。假设要去除index 2的字符

1.用StringBuffer.remove(int index)

  String st = "abcd";

  StringBuffer sb = new StringBuffer(st); // String 转为 StringBuffer

  sb.remove(2); // 用StringBuffer中的remove方法删除指定位置字符 

  String re1 = sb.toString();  // StringBuffer 转回 String

  System.out.println(re1); // abd

2.用String类的subString方法拼接

  String st = "abcd";

  String re2 = st.substring(0, 2) + st.substring(2 + 1);

  System.out.println(re2); // abd

posted @ 2020-04-23 14:42  mayingdts  阅读(4238)  评论(0编辑  收藏  举报