Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2006
    Posts
    131
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Getting Name initials

    hello...

    How do I get the initials from a String that contains a name?

    IE.

    String name = Bob Martin John Lee;
    initials = name(?????)

    having initials = BMJL.

    thanks
    ---------
    Client / Server Side Programmer II

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ynotlim View Post
    hello...

    How do I get the initials from a String that contains a name?

    IE.

    String name = Bob Martin John Lee;
    initials = name(?????)

    having initials = BMJL.

    thanks
    I assume you are talking something like this??
    Code:
    String name = "Bob Martin John Lee";
    
    String initials = name.substring(0, 1) + name.substring(4, 5) 
    + name.substring(11, 12) + name.substring(16, 17);
    		
    System.out.println(initials);
    This is the quick and dirty way to do it..

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by TechGlider View Post
    I assume you are talking something like this??
    Code:
    String name = "Bob Martin John Lee";
    
    String initials = name.substring(0, 1) + name.substring(4, 5) 
    + name.substring(11, 12) + name.substring(16, 17);
    		
    System.out.println(initials);
    This is the quick and dirty way to do it..
    T'will only work for that example though (uses contant magic numbers).
    There are a few ways to do this, iteration is always an option (when in doubt), but I would look at <string>.split.
    Something like this perhaps (untested):
    Code:
    String name = "Bob Martin John Lee";
    StringBuilder sbInitials = new StringBuilder();
    String[] nameParts = name.split("\s");
    for (String part : nameParts)
    {
        sbInitials.append(part.charAt(0));
    }
    String initials = sbInitials.toString();
    I'm not 100% certain that the regex for the split will work with \s in java. Give it a shot and post back you're results.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    T'will only work for that example though (uses contant magic numbers).
    There are a few ways to do this, iteration is always an option (when in doubt), but I would look at <string>.split.
    Something like this perhaps (untested):
    Code:
    String name = "Bob Martin John Lee";
    StringBuilder sbInitials = new StringBuilder();
    String[] nameParts = name.split("\s");
    for (String part : nameParts)
    {
        sbInitials.append(part.charAt(0));
    }
    String initials = sbInitials.toString();
    I'm not 100% certain that the regex for the split will work with \s in java. Give it a shot and post back you're results.
    Also correct, i was just doing it for that string and it makes it unique to only that string, now if you want to do it for every single instance you have a name and want to extract all of the initials, your talking regular expressions.
    Fou-lu's example should work and i am also not positive about java accepting the "\s", but it will be something you will need to store in an array/vector, if you have multiple names being parsed out like that.'

    Also, you might not understand Fou-lu's "for (String part : nameParts)" which is an enhanced for loop in the new java framework.. Check it out, can be really helpful.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yes, I always forget to mention that (1.5 I believe is when it came into effect). Its simply java's response to scripting languages 'foreach' calls that automatically assign each value to a variable in the actual loop condition.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by Fou-Lu View Post
    I'm not 100% certain that the regex for the split will work with \s in java. Give it a shot and post back you're results.
    you're 100% right...it won't you have to escape the backslashes for java (makes them terrible to read though...)

    Code:
    nameParts = name.split("\\s");
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #7
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    whats wrong with:

    Code:
    name.split(" ");

  • #8
    New Coder
    Join Date
    Nov 2008
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brad211987 View Post
    whats wrong with:

    Code:
    name.split(" ");
    Both of these will work:

    Code:
    	public static void main(String[] args) {
    		String name = "Bob Martin John Lee";
    		StringBuilder sbInitials = new StringBuilder();
    		String[] nameParts = name.split("\\s");
    		for (String part : nameParts)
    		{
    		    sbInitials.append(part.charAt(0));
    		}
    		String initials = sbInitials.toString();
    		System.out.println(initials);
    	}
    Code:
    	public static void main(String[] args) {
    		String name = "Bob Martin John Lee";
    		StringBuilder sbInitials = new StringBuilder();
    		String[] nameParts = name.split(" ");
    		for (String part : nameParts)
    		{
    		    sbInitials.append(part.charAt(0));
    		}
    		String initials = sbInitials.toString();
    		System.out.println(initials);
    	}

  • #9
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by brad211987 View Post
    whats wrong with:

    Code:
    name.split(" ");
    not esoteric enough
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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