Results 1 to 4 of 4
  1. #1
    kDude is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default Removing characters

    So I'm trying to take a string ex. 'Lewd did I live, & evil I did dwel.' and remove all the spaces and special characters so that it just reads 'lewddidiliveevilididdwel' Here's what I have so far...I found this code on a site and it works for removing one character, but how would I edit it so it removes multiple characters? I tried adding more loops and editing it, but haven't been able to make it work. Someone help me out?
    Java Code:
        public static String removeChar(String hello, char a) 
        {   
            String x = "";
            for (int i = 0; i < hello.length(); i ++) 
            {
                if (hello.charAt(i) != a)
                {
                    x += hello.charAt(i);
                }
            }
            return x;  
        }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
            String s = "Lewd did I live, & evil I did dwel";
            System.out.println("s = " + s);
            char space = ' ';
            s = removeChar(s, space);
            System.out.println("s = " + s);
            String s2 = "Replaces each substring of this string that " +
                        "matches the literal target sequence with the " +
                        "specified literal replacement sequence." +
            System.out.println("s2 = " + s2);
            s2 = s2.replace(" ", "");
            System.out.println("s2 = " + s2);

  3. #3
    kDude is offline Member
    Join Date
    Dec 2007
    Posts
    2
    Rep Power
    0

    Default

    Thanks, I understand how it works, but how do I use that in my code?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    how do I use that in my code?
    You can use the String replace method in your code. Or you can use the replaceChar method or you can use the loop from the replaceChar method in your code.

Similar Threads

  1. image removing
    By Triss in forum New To Java
    Replies: 3
    Last Post: 01-20-2008, 08:27 PM
  2. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 04:05 PM
  3. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM
  4. removing an object from memory
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 09:23 PM
  5. Help with removing somthing once inputed.
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-26-2007, 10:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •