-
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
-
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
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks