Results 1 to 4 of 4

Thread: Java - remove letters from string

  1. #1
    Join Date
    Aug 2009
    Posts
    10

    Java - remove letters from string

    Hi,

    I am new to java. I want to know how you guys remove the letters in a string?
    Can someone please help me out?

  2. #2
    Join Date
    Apr 2008
    Posts
    55

    Re: Java - remove letters from string

    Calling following method with any String will return a numbers-only string.

    Code:
    public static String getOnlyNumerics(String str) {
        
        if (str == null) {
            return null;
        }
    
        StringBuffer strBuff = new StringBuffer();
        char c;
        
        for (int i = 0; i < str.length() ; i++) {
            c = str.charAt(i);
            
            if (Character.isDigit(c)) {
                strBuff.append(c);
            }
        }
        return strBuff.toString();
    }
    Hope this helps.

  3. #3
    Join Date
    May 2008
    Posts
    41

    Re: Java - remove letters from string

    Try this:

    Code:
    for (int j = 0; j < keyword.length(); j++)
        {
            letter = keyword.charAt(j);
             for (int i = 0; i < alphabet.length(); i ++)
                 { 
                   if (alphabet.charAt(i) != letter) 
                    {
                      r += alphabet.charAt(i);
                    }
                 }
        }

  4. #4
    Join Date
    Apr 2008
    Posts
    32

    Re: Java - remove letters from string

    To extract or remove a String.

    Code:
    public class Sample1 {
    
        public static void main(String[] args) {
    
            String name = "chickens";
    
            String extracted1,
                   extracted2,
                   extracted3;
    
            extracted1 = name.substring(1);
            extracted2 = name.substring(2);
            extracted3 = name.substring(3);
    
            System.out.println(extracted1);
            System.out.println(extracted2);
            System.out.println(extracted3);
        }
    }

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. how to remove certain characters from a String in java
    By Baazigar in forum Software Development
    Replies: 3
    Last Post: 13-02-2010, 08:02 PM
  3. Java String test
    By Aidan 12 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 02:22 PM
  4. How to remove Drive letters using GPO
    By Ayush in forum Operating Systems
    Replies: 4
    Last Post: 17-04-2009, 01:22 PM
  5. Replies: 2
    Last Post: 04-03-2008, 09:50 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,751,747,922.74681 seconds with 16 queries