[Basic Interview Questions] Delete specified characters from JAVA-String (11 methods)

The first method-traverse from front to back through a loop, if it is not the character to be deleted, add it to the processed string, the code is as follows:

public String deleteCharString0(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    for (int i = 0; i < sourceString.length(); i++) {
    
    
        if (sourceString.charAt(i) != chElemData) {
    
    
            deleteString += sourceString.charAt(i);
        }
    }
    return deleteString;
}

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

public String deleteCharString1(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    int iIndex = 0;
    for (int i = 0; i < sourceString.length(); i++) {
    
    
        if (sourceString.charAt(i) == chElemData) {
    
    
            if (i > 0) {
    
    
                deleteString += sourceString.substring(iIndex, i);
            }
            iIndex = i + 1;
        }
    }
    if (iIndex <= sourceString.length()) {
    
    
        deleteString += sourceString.substring(iIndex, sourceString.length());
    }
    return deleteString;
}

The third method-the principle is the same as above, except that the function in the String class is used to find the position of the character to be deleted. The efficiency is not as high as the above. The code is as follows:

public String deleteCharString2(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    int iIndex = 0;
    int tmpCount = 0;
    do {
    
    
        tmpCount = sourceString.indexOf(chElemData, iIndex);
        if (tmpCount > 0) {
    
    
            deleteString += sourceString.substring(iIndex, tmpCount);
        }
        if (tmpCount != -1) {
    
    
            iIndex = tmpCount + 1;
        }
    } while (tmpCount != -1);
    if (iIndex <= sourceString.length()) {
    
    
        deleteString += sourceString.substring(iIndex, sourceString.length());
    }
    return deleteString;
}

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

public String deleteCharString3(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    int iIndex = sourceString.length();
    int tmpCount = 0;
    do {
    
    
        tmpCount = sourceString.lastIndexOf(chElemData, iIndex - 1);
        if (tmpCount < sourceString.length() && tmpCount >= 0) {
    
    
            deleteString = sourceString.substring(tmpCount + 1, iIndex) + deleteString;
        }
        if (tmpCount != -1) {
    
    
            iIndex = tmpCount;
        }
    } while (tmpCount != -1);
    if (iIndex >= 0) {
    
    
        deleteString = sourceString.substring(0, iIndex) + deleteString;
    }

    return deleteString;
}

The fifth method-by using the regular method and the replaceAll function, this method should pay attention to special characters, such as the "." character in the regular, which needs to be escaped. The code is as follows:

public String deleteCharString4(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    final String strTable = "|^$*+?.(){}\\";
    String tmpRegex = "["; 
    for (int i = 0; i < strTable.length(); i++) {
    
    
        if (strTable.charAt(i) == chElemData) {
    
    
            tmpRegex += "\\";
            break;
        }
    }
    tmpRegex += chElemData + "]";
    deleteString = sourceString.replaceAll(tmpRegex, "");
    return deleteString;
}

The sixth method-split the string into several substrings in a regular manner, and then concatenate the substrings, the code is as follows:

public String deleteCharString5(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    final String strTable = "|^$*+?.(){}\\";
    String tmpRegex = "["; 
    for (int i = 0; i < strTable.length(); i++) {
    
    
        if (strTable.charAt(i) == chElemData) {
    
    
            tmpRegex += "\\";
            break;
        }
    }
    tmpRegex += chElemData + "]";
    String[] tmpStringArray = sourceString.split(tmpRegex);
    for (int i = 0; i < tmpStringArray.length; i++) {
    
    
        deleteString += tmpStringArray[i];
    }
    return deleteString;
}

The seventh method—program a readable sequence of characters and replace them with methods in the String class. The code is as follows:

public String deleteCharString6(String sourceString, char chElemData) {
    
    
    String tmpString = "";
    tmpString += chElemData;
    tmpString.subSequence(0, 0);
    String deleteString = "";
    deleteString = sourceString.replace(tmpString, deleteString.subSequence(0, 0));
    return deleteString;
}

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

public String deleteCharString7(String sourceString, char chElemData) {
    
    
    String deleteString = "";
    char[] Bytes = sourceString.toCharArray();
    int iSize = Bytes.length;
    for (int i = Bytes.length - 1; i >= 0; i--) {
    
    
        if (Bytes[i] == chElemData) {
    
    
            for (int j = i; j < iSize - 1; j++) {
    
    
                Bytes[j] = Bytes[j + 1];
            }
            iSize--;
        }
    }
    for (int i = 0; i < iSize; i++) {
    
    
        deleteString += Bytes[i];
    }
    return deleteString;
}

The ninth method-the principle is similar to the first method. This time, the append method in the stringBuffer class is used for operation. I think the efficiency should be higher than the first method.

public String deleteCharString8(String sourceString, char chElemData) {
    
    
    StringBuffer stringBuffer = new StringBuffer("");
    for (int i = 0; i < sourceString.length(); i++) {
    
    
        if (sourceString.charAt(i) != chElemData) {
    
    
            stringBuffer.append(sourceString.charAt(i));
        }
    }
    return stringBuffer.toString();
}

The tenth method-use the replace and indexOf method in the stringBuffer class ( _ deliberately collect method), the code is as follows:

public String deleteCharString9(String sourceString, char chElemData) {
    
    
    String tmpString = "";
    tmpString += chElemData;
    StringBuffer stringBuffer = new StringBuffer(sourceString);
    int iFlag = -1;
    do {
    
    
        iFlag = stringBuffer.indexOf(tmpString);
        if (iFlag != -1) {
    
    
            stringBuffer = stringBuffer.replace(iFlag, iFlag + 1, "");
        }
    } while (iFlag != -1);
    return stringBuffer.toString();
}

The eleventh method-use deleteCharAt and indexOf in the stringBuffer class to delete directly

public String deleteCharString10(String sourceString, char chElemData) {
    
    
    String tmpString = "";
    tmpString += chElemData;
    StringBuffer stringBuffer = new StringBuffer(sourceString);
    int iFlag = -1;
    do {
    
    
        iFlag = stringBuffer.indexOf(tmpString);
        if (iFlag != -1) {
    
    
            stringBuffer.deleteCharAt(iFlag);
        }
    } while (iFlag != -1);
    return stringBuffer.toString();
}

Screenshot of program running:

Insert picture description here

How to remove the specified characters in JAVA String

With the help of jdk java.lang.String.replace(CharSequence, CharSequence) method
Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36963950/article/details/107380223