Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 01:16 AM
Member
 
Join Date: Dec 2007
Posts: 2
Rep Power: 0
kDude is on a distinguished road
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?
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;  
    }
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-03-2007, 02:24 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
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);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-03-2007, 02:34 AM
Member
 
Join Date: Dec 2007
Posts: 2
Rep Power: 0
kDude is on a distinguished road
Default
Thanks, I understand how it works, but how do I use that in my code?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-03-2007, 03:38 AM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
image removing Triss New To Java 3 01-20-2008 09:27 PM
Removing components from JPanel Echilon New To Java 0 12-30-2007 05:05 PM
removing duplicates from arrays bugger New To Java 3 11-13-2007 07:11 PM
removing an object from memory ravian New To Java 1 11-12-2007 10:23 PM
Help with removing somthing once inputed. fegiflu New To Java 8 07-26-2007 11:58 PM


All times are GMT +2. The time now is 03:07 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org