There is a simpler method: Copy the code code as follows: str1="abcd"; str2="cd"; str3=str1.replaceAll(str2,""); //str3="ab"