Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-03-2007, 01:16 AM
Member
 
Join Date: Dec 2007
Posts: 2
kDude is on a distinguished road
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
Sponsored Links
  #2 (permalink)  
Old 12-03-2007, 02:24 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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
kDude is on a distinguished road
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
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 06:10 PM.


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