Remove any given character from a String

public class RemoveChar4mString {

    public static void main (String[] args)
    {
        System.out.println (removeChar4mString ("My blog name is Sourcecode-kk",  'o'));
    }

    public static String removeChar4mString (String str, char charToBeRemoved)
    {
        if (str == null)
            return "";

        StringBuilder strBuild = new StringBuilder ();

        for (int i = 0; i < str.length (); i++)
        {
            char ch = str.charAt (i);
            if (ch == charToBeRemoved)
                continue;
            strBuild.append (ch);
        }
        return strBuild.toString ();
    }
}

output:-

My blg name is Surcecde-kk

this code removes all "o" character from given string "My Blog name is Sourcecode-kk"


0 comments :