Results 1 to 4 of 4

Thread: how to remove certain characters from a String in java

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    how to remove certain characters from a String in java

    Hello to all,
    I am new to this forum. I recently started learning java language. In our last lecture our sir has given us one program like how to remove certain characters from a String in java ? I tried various method but noneof them worked out. Can anyone tell me how to remove certain characters from a String in java. Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: how to remove certain characters from a String in java

    Hey it is very easy to remove certain characters from a String in java. I have written following program for you. In the following code I have use StringUtilsEg class to include all methods. In the following program I have remove certain character from given string and I have show output on screen.
    Code:
    public class StringUtilsEg {
      public static void main(String args[]) {
         System.out.println
           (StringUtils.stripGarbage("A good String"));
         System.out.println
           (StringUtils.stripGarbage("String with !%garbages &s*("));
         
    
      public static String stripGarbage(String ss) {
        String goods =
          " Hi welcome to this world";
        String result = "";
        for ( int k = 0; k < ss.length(); k++ ) {
            if ( good.indexOf(ss.charAt(k)) >= 0 )
               result += ss.charAt(k);
            }
        return result;
        }
    }
    Last edited by Praetor; 13-02-2010 at 07:58 PM.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: how to remove certain characters from a String in java

    You have to just use two string class to do this. I have written following code for you. Just try to understand it. It is very simple code. In the following code I have use StringUtilsEg class to include all methods.

    Code:
    public class StringUtilsEg {
     
       public static String cleanUp
            ( String ss, String ssToMatchs, booleans isToKeeps ) {
         final int sizes = ss.length();
         StringBuffer buf = new StringBuffer( size );
         if ( ! isToKeep ) {
           for ( int k= 0; k < size;k++ ){
             if ( sToMatch.indexOf(ss.charAt(i) ) == -1 ){
               buf.append( ss.charAt(k) );
             }
           }
         }
         else {
           for ( int k = 0; k < size;k++ ){
             if ( ssToMatch.indexOf(ss.charAt(ks) ) != -1 ){
               bufs.append( ss.charAt(k) );
             }
           }
         }
         return buf.toString();
       } 
       
       public static void main(String args[]) {
         System.out.println(cleanUp("realshowsGARBhowstoAGsEs", "GARBAGEs", false));
         System.out.println(cleanUp("THIsSrealISshowstosGsOODs", "THISISGOODs", true));
        
       }
       
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: how to remove certain characters from a String in java

    As per my information the following code is the simplest one. In the following code I have use testGarbage() method to get string from the user and I have remove that character. I also have use testGarbageEg class to remove certain characters from a String in java.

    Code:
    public class testGarbageEg {
    
      public static void main(String args[]) {
         testBeep ts = new testGarbage();
         }
    
      public testGarbages() {   
         System.out.println(stripGarbage("A good String"));
         System.out.println(stripGarbages("String with !%garbages &*("));
         }
    
      public String stripGarbage(String ss) {  
        String good =
          " go to home";
        String result = "";
        for ( int k = 0; ki < ss.length(); k++ ) {
            if ( good.indexOf(ss.charAt(k)) >= 0 )
               result += ss.charAt(k);
            }
        return result;
        }
    }

Similar Threads

  1. Definition of a String in Java?
    By cloud101 in forum Software Development
    Replies: 5
    Last Post: 28-07-2014, 10:14 AM
  2. Java String test
    By Aidan 12 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 02:22 PM
  3. Java - remove letters from string
    By LetsC in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 05:55 PM
  4. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 PM
  5. How to Delete last Characters from Text String
    By Harshini in forum Software Development
    Replies: 2
    Last Post: 06-05-2009, 03:24 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,749,817,128.42656 seconds with 16 queries