Results 1 to 9 of 9
  1. #1
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default how to get initials

    i want to get the initials of 1st two word from a string
    eg: abhijit prakash bagal
    i want to display it as a.p bagal.
    can any one give me method for it?

  2. #2
    Join Date
    Dec 2011
    Posts
    11
    Rep Power
    0

    Default Re: how to get initials

    you can use string buffers but you would need to put a little bit of work into it. here is where i did my tutorial when i first tried a similar thing How to use the Java StringBuffer class - YouTube

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to get initials

    Quote Originally Posted by abhi7080 View Post
    i want to get the initials of 1st two word from a string
    eg: abhijit prakash bagal
    i want to display it as a.p bagal.
    can any one give me method for it?
    That's not the purpose of this forum -- to give you the code you desire or to do your assignments for you. Rather we'd be more than happy to help you learn, and so in that vein, why not first try to solve this yourself -- it's not that hard to do, and then if your code works, great, but let us know. If it doesn't then please post your code here with code tags and with any error messages that it might cause, and let's see if we can help you out.

  4. #4
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default Re: how to get initials

    Quote Originally Posted by Fubarable View Post
    That's not the purpose of this forum -- to give you the code you desire or to do your assignments for you. Rather we'd be more than happy to help you learn, and so in that vein, why not first try to solve this yourself -- it's not that hard to do, and then if your code works, great, but let us know. If it doesn't then please post your code here with code tags and with any error messages that it might cause, and let's see if we can help you out.
    OK
    thanks for reply,
    i hv tried following code to find initials but that is not working pls solve following .

    Java Code:
     
    String name = "Abhijit peakash bagal";
    StringBuilder sbInitials = new StringBuilder();
    String[] nameParts = name.split("\s");
    for (String part : nameParts) {
        sbInitials.append(part.charAt(0));
    }
    String initials = sbInitials.toString();
    Last edited by Eranga; 01-06-2012 at 03:22 AM. Reason: code tags added

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to get initials

    for starters, you need to use code tags. Secondly, you didn't post your error. Thirdly, you didn't escape your \. Lastly, this program works fine for me!

  6. #6
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

    Default Re: how to get initials

    i am getting error : no escape character (for "\s").

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: how to get initials

    Do you remember all the escape characters we are using? Is there such character like "\s"?

    Did you read about split() in Java DOC?

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: how to get initials

    i am getting error : no escape character (for "\s").
    Did you read my post? You need to double escape that, so "\s" becomes "\\s". Java doesn't have a \s escape character but regex does. You're writing a regex. So to prevent java from thinking it is an escape character for java, you need to escape the escape character so it gets passed cleanly to regex. In other words, double escape \\s.

  9. #9
    abhi7080 is offline Member
    Join Date
    Dec 2011
    Location
    india
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Initials (what am i doing wrong)
    By genocist in forum New To Java
    Replies: 5
    Last Post: 05-10-2010, 11:08 PM
  2. how do i initials the word so i get the first letter only
    By mikeitalydz in forum New To Java
    Replies: 15
    Last Post: 04-19-2009, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •