Selecting part of a string


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Selecting part of a string

  1. #1
    Simon Appleyard Guest

    Selecting part of a string


    I have a string such as "S. J. Brown". Is it possible to select only the
    "Brown" part of the string. I cannot use substring because I will not know
    how many initials there are. Is there a way to select the surname from any
    name.

    Thank You

  2. #2
    Paul Clapham Guest

    Re: Selecting part of a string

    Depends how good you want your algorithm to be. The simple algorithm is to
    just scan from the right until you reach something that isn't a letter. Of
    course this fails if the person has only a surname (Christo). And it fails
    if the person's name is hyphenated (Browne-Staines) or has punctuation
    (O'Malley). And it fails if the person's surname has more than one word (De
    Moivre, Van de Wetering). And it fails if the person's surname comes at the
    beginning (Deng Xiao Ping). And it fails if the person doesn't have a
    surname (Cher, Madonna). So as you see the short answer to your question is
    "No". But if your names come from a restricted environment, you may be able
    to use something like "scan from the right until you reach a blank or a
    period or the beginning of the string".

    Simon Appleyard <simon@appleyard1.karoo.co.uk> wrote in message
    news:38df732a$1@news.devx.com...
    >
    > I have a string such as "S. J. Brown". Is it possible to select only the
    > "Brown" part of the string. I cannot use substring because I will not

    know
    > how many initials there are. Is there a way to select the surname from

    any
    > name.
    >
    > Thank You




  3. #3
    John Timney (MVP) Guest

    Re: Selecting part of a string

    I dont have an example to hand, but look up the StringTokenizer, it can take
    a string and break it up based on a common delimter such as the "." between
    the J and the B

    Regards

    John Timney (MVP)


    Simon Appleyard <simon@appleyard1.karoo.co.uk> wrote in message
    news:38df732a$1@news.devx.com...
    >
    > I have a string such as "S. J. Brown". Is it possible to select only the
    > "Brown" part of the string. I cannot use substring because I will not

    know
    > how many initials there are. Is there a way to select the surname from

    any
    > name.
    >
    > Thank You




  4. #4
    Paul Balmforth Guest

    Re: Selecting part of a string



    Hi there.

    This should be no problem, but depends what you can assume
    about the format of the original name. It's a simple scan
    from the right for the correct position to do the substring.
    The following code should handle these cases:

    "J. R. Hartley-Smith "
    "JR Hartley-Smith"
    "J.R.Hartley-Smith"

    ===

    public static final String getSurname(String fullName) {

    // remove spaces from the end
    while ((fullName.length() > 1) &&
    (fullName.endsWith(" ")) {
    fullName = fullName.substring(0, fullName.length()-1);
    }

    // get the substring
    int posForSub = Math.max(
    Math.max(fullName.lastIndexOf(' '),
    fullName.lastIndexOf('.')), 0);

    if (posForSub == (fullName.length()-1)) {
    return "";
    }
    else {
    return fullName.substring(posForSub+1,
    fullName.length());
    }
    }

    "Simon Appleyard" <simon@appleyard1.karoo.co.uk> wrote:
    >
    >I have a string such as "S. J. Brown". Is it possible to select only the
    >"Brown" part of the string. I cannot use substring because I will not know
    >how many initials there are. Is there a way to select the surname from

    any
    >name.
    >
    >Thank You



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center