How to find first space in string name

N

naeron

Hi,

I am new to java programming .I have this programme for which i am
entering the first name and last name via a string e.g Bob Thomas and
then sorting the names.Can some body tell me that after entering the
name how can i find the space in the string e.g how should i program
that after Bob if there is a space that means it was the first name.

Thanks
 
M

Michael Rauscher

Hi,

I am new to java programming .I have this programme for which i am
entering the first name and last name via a string e.g Bob Thomas and
then sorting the names.Can some body tell me that after entering the
name how can i find the space in the string e.g how should i program
that after Bob if there is a space that means it was the first name.

Have a look at String#indexOf.

Bye
Michael
 
S

Simon Brooke

in message <[email protected]>,
Hi,

I am new to java programming .I have this programme for which i am
entering the first name and last name via a string e.g Bob Thomas and
then sorting the names.Can some body tell me that after entering the
name how can i find the space in the string e.g how should i program
that after Bob if there is a space that means it was the first name.

To find the remainder of a string after the first space

String name = "Bob Thomas";
String surname = name;
int offset = name.indexOf( ' ');

if ( offset > -1)
{
surname = name.substring( space + 1);
}

To find the remainder after the /last/ space (which is I think what you
really want, substitute lastIndexOf for indexOf in the above. However, to
protect yourself from bad formatting which has spaces on either the
beginning or the end of the string, I'd use

public String surname( String name)
{
String result = name;

try
{
result = name.trim();
result = result.substring( result.lastIndexOf( ' ') + 1);
}
catch ( Exception any)
{
/* NullPointer (possible) or IndexOutOfBounds (shouldn't
happen); in any case, ignore */
}

return result;
}

Not testing for validity but just letting the exception handler catch
problems saves a few cycles on every 'good' call, at a considerable cost
on every 'bad' call; but if you're reasonably sure the input data is in
reasonable state it's probably a saving over all.
 
M

mcltunes

If it were me, I'd probably use StringTokenizer. Check the API
javadocs for usage.

-- Mark
 
A

Andrew Thompson

mcltunes wrote:

Please refrain from top-posting.
If it were me,

2 points.
1) It is 'not you'
2) Applications should be developed with a view not
to you nor me, nor the developer, but to the target
end-user, for the particular use.
...I'd probably use StringTokenizer.

That is compatible with old Java, but so is
String.indexOf (and its variants).

*It would probably be fine for this simple useage*
but note..
...Check the API
javadocs for usage.

...and then check the Bug Database, for implementation
quirks between different Java versions, and usenet, for
posts from its many critics. ;-)

Generally, for a 1.4+ application, String.split() is the
recommended way to get the parts of a string.

(Though for this instance, I would guess that .indexOf
is fastest for hundreds of records, but would do a simple
test to check it, if it came to that).

Andrew T.
 
M

mcltunes

Andrew said:
mcltunes wrote:

Please refrain from top-posting.

This group has certainly gotten more caustic since I last posted here.
Good luck newbies. I hope the rude guys don't discourage you.
2 points.
1) It is 'not you'

Still I've used StringTokenizer with much success, and this developer
likely will too based on the info provided.
2) Applications should be developed with a view not
to you nor me, nor the developer, but to the target
end-user, for the particular use.

The target end user doesn't care how the developer parses a string,
just so they parse it correctly without blowing up the system.

fwiw -- I checked the API javadocs for StringTokenizer which indicates
that although not deprecated yet, this class is for legacy support, and
recommends using String.split() wherever possible.

-- Mark
 
S

Simon Brooke

Still I've used StringTokenizer with much success, and this developer
likely will too based on the info provided.

StringTokenizer is a reasonable solution, but you do have the overhead of
creating and garbage collecting another object. String.indexOf() is
computationally simple and will work, and even iterating down a string
directly is likely to be computationally simpler than using a
StringTokenizer. I agree a StringTokenizer is a good solution in its
place, but I suggest its place is rather more complex problems than this.

String.split() I haven't yet enough familiarity with to comment, since I'm
still developing stuff which has to be able to work with 1.3. Backwards
compatibility is not a bad thing in itself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top