Java中如何从字符串中删除特定字符

17

例如,假设给定一个字符串 Battle of the Vowels:Hawaii vs Gronzy,当我们指定要删除的字符为aeiou时,该函数应将字符串转换为 Bttl f th V wls:Hw vs Grzny

这个问题出现在书籍Programming Interviews Exposed中。书中是用 C 语言解释的,不过我对 Java 感兴趣。


2
你打算尝试实现这个吗?你到目前为止尝试了什么?我们不会替你完成。 - Greg Hewgill
我向社区征求了实现它的不同方法的想法。 - SuperMan
未来的问题真棒 - vikramvi
4个回答

35

我尝试了这个:s1 = s1.replaceAll(s2, ""); System.out.println(s1); 但它不起作用。看起来这个解决方案只在指定要删除的字符时才有效。 - Hengameh

4
public class RemoveChars {

    char[] replaceChar = {'a','e','i','o','u'};

    public static void main(String[] args) {
        String src = "Battle of the Vowels:Hawaii vs Gronzy";
        System.out.println(new RemoveChars().removeChar(src));
    }

    public String removeChar(String src){
        char[] srcArr = src.toCharArray(); 
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < srcArr.length; i++) {
            char foundChar = isFound(srcArr[i]);
            if(foundChar!='\0')
            sb.append(foundChar);           
        }
        return sb.toString();

    } 

    public char isFound(char src){      
        for (int i = 0; i < replaceChar.length; i++) {
            if(src==replaceChar[i]){
                return '\0';
            }
        }
        return src;
    }
}

0
String str = "sandeep";
StringBuilder sb = new StringBuilder();

System.out.println("Enter character to removed: ");
String ch = new Scanner(System.in).next();
for (int i = 0; i < str.length(); i++)
{
    if (str.charAt(i) == ch.charAt(0))
    {
        continue;
    }
    sb.append(str.charAt(i));
}
System.out.println(sb);

0
public class RemoveCharacters
{
    static String removeCharsFromString(String word1, String word2)
    {
        StringBuilder sb = new StringBuilder(word1);

        System.out.println(sb);
        //char[] word2characters= word2.toCharArray();
        HashMap<Character, Integer> table = new HashMap<Character, Integer>();

        for (int i = 0; i < word2.length(); i++)
        {
            table.put(word2.charAt(i), 1);

        }

        int p = 0;
        for (int i = 0; i < word1.length(); i++)
        {

            if (table.containsKey(word1.charAt(i)))
            {
                if (p == 0)
                {
                    sb.deleteCharAt(i);
                    //p++;
                }
                else
                {
                    sb.deleteCharAt(i - p);
                }
                //System.out.println(sb);
                p++;
            }

        }

        return sb.toString();
    }

    public static void main(String[] args)
    {
        System.out.println("Enter your string");
        Scanner sc = new Scanner(System.in);
        String originalword = sc.nextLine();

        System.out.println("Enter the remove string");
        Scanner sc1 = new Scanner(System.in);
        String removecharacters = sc1.nextLine();

        String result = removeCharsFromString(originalword, removecharacters);

        System.out.println(result);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接