Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: How to remove letters

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to remove letters

    how do you remove letters in a string?


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Good day, gentlemen.

    Hello!
    I'm new here, just sign up today! Hoping to make new friends!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to remove letters

    Create a new string that doesn't contain the character in question
    public static String removeChar(String str, int position)
    {
         return str.subString(0,position)+str.subString(position,str.length());
    }

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    this might help you a little bit
    public class StringFormat3 {
     
        public static void main(String[] args) {
     
            String name;
     
            name = "zabdiel";
     
            //dont bother this part.. its only for measuring the lenght of a strings ,using .length() method.
            System.out.println("Your name consists of " + name.length() + " characters");
     
            //the following statements shows how to extract a character or a String in a String.
            System.out.println(name.substring(0,1));
            System.out.println(name.substring(1,2));
            System.out.println(name.substring(2,3));
            System.out.println(name.substring(3,4));
            System.out.println(name.substring(4,5));
            System.out.println(name.substring(5,6));
            System.out.println(name.substring(6,7));
        }
    }

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    another example..

    this one will show you how to extract or remove a String.
    this one differs to my first example..
    public class Sample1 {
     
        public static void main(String[] args) {
     
            String name = "Zabdiel";
     
            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);
        }
    }

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to remove letters

    How about using a StringBuilder?

    StringBuilder (Java Platform SE 6), int)

    // Json

  7. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    ei.. in string builder? extract a string? can you please give a little example of that using stringbuilder sir?
    tnx in advance!!

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to remove letters

    How about this then.

            final StringBuilder stringBuilder = new StringBuilder("Please remove me");
            System.out.println(stringBuilder.toString());
     
            stringBuilder.delete(7, 14);
            System.out.println(stringBuilder.toString());

    Output:

    Please remove me
    Please me
    // Json

  9. #9
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    ahh .delete() method.. whaa didnt notice it

    hmmm ill keep that one

  10. #10
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    ahh theres a difference in .substring() method and .delete() method..

    in substring() method it extracts the location of the specified character or string.
    while in delete() method it deletes or removes the excess character or string behind the corresponding character that the users wants to display.

    do i make sense sir?

    hehe

    StringBuilder.delete(intial value of the character that wil be displayed , the succeeding values that will be deleted);

    StringBuilder stringBuilder = new StringBuilder("zabdiel");
    stringBuilder.delete(1,7);

    output:

    z
    Last edited by chronoz13; October 3rd, 2009 at 04:39 AM.

  11. #11
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How to remove letters

    delete is only available on the StringBuilder though I believe.

    // Json

  12. #12
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to remove letters

    use BufferString class is a good idea.

  13. #13
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: How to remove letters

    how sir? can you show a little example?

  14. #14
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to remove letters

    for example:if you want remove letters "ge" from string "gehaiming",you could do like the follow code.
    StringBuffer sb=new StringBuffer("gehaiming");
    sb=sb.replace(0,2,"");

Similar Threads

  1. Remove from array after a period of time
    By Marty in forum Collections and Generics
    Replies: 1
    Last Post: September 1st, 2009, 07:57 AM