String delete character

The String class does not provide a delete class method, but deleting characters is a common requirement.

String st = "abcd";

1. Delete specific characters

This is a bit complicated. Reprinted directly, see https://blog.csdn.net/linzhiqiang0316/article/details/90440696 for the original text  :

the first method

Traverse through the loop from front to back, if it is not the character to be deleted, it will be added to the processed string, the code is as follows:

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     }

The second method

Determine the position index of the character to be deleted by looping, and then join the substrings by splitting the string form. Note that there is no character to be deleted in the last substring and source string. The code is as follows:

 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     }

The third method

The principle is the same as above, except that the position of the character to be deleted is executed using the function in the String class, and the efficiency is not as high as the above, the code is as follows:

 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     }

 

The fourth method

The principle is basically the same as above, but this time using the reverse order method, there are more pits here, we must pay attention to the index value range and whether it is legal, the code is as follows:

 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     }

 

The fifth method

By using the regular way and the replaceAll function, this method should pay attention to special characters, such as the "." Character in the regular, you need to escape the special characters, the code is as follows:

 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     }

 

The sixth method

Use a regular way to split the string into several substrings, and then splice the substrings, the code is as follows:

 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     }

 

The seventh method

The characters can be programmed into readable sequences and replaced by methods in the String class. The code is as follows:

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     }

 

Eighth method

The original string is converted into a character array, and then the principle is similar to the principle of direct insertion sort, the code is as follows:

 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     }

 

Ninth method

The principle is similar to the first method, this time using the append method in the stringBuffer class to operate, I think the efficiency should be higher than the first.

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     }

 

Tenth method

Using the replace and indexOf method in the stringBuffer class (_ deliberately make method), the code is as follows:

 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     }

 

The eleventh method

Use deleteCharAt and indexOf in the stringBuffer class to delete directly

 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     }

 

Second, delete the character at the specified position

This is simple. Suppose you want to remove the characters of index 2

1.用StringBuffer.remove(int index)

  String st = "abcd";

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

  sb.remove (2); // Use the remove method in StringBuffer to delete the character at the specified position 

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

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

2. Use the String substring method to stitch

  String st = "abcd";

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

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

Guess you like

Origin www.cnblogs.com/mayingdts/p/12760696.html