Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
The string we use, referred to by stremail.
== IMPORTANT ==
An accurate way to see if strings are equal .
We determine the number of characters it contains.
What's the @'s position ? First character is zero.
The single qoutes are no typo, they're used to denote a character literal.
The indexOf method returns -1 if not found.
That's not the case in our example, of course, but if it were,...
the actionPerformed method that we're in would have been left.
We declare two String reference variables.
We call String's substring method to get the email adress' left side, from the first character, 0, to the @, not included.
Same, but from the character next to the @ until the end of the string.
The following statement will result in false more often than not, because you're comparing two references/pointers to strings, NOT the values of the strings !!!!
if ( str == "Doink" )
So this just DOESN'T WORK !
The String class contains many more interesting methods, like :
- equalsIgnoreCase ( String )
- indexOf ( String )
lookup a string.
- indexOf ( String, int startpos )
lookup string from startpos.
- lastIndexOf ( String )
start at the righthand side.
- startsWith ( String )
- endsWith ( String )
- toLowerCase( )
makes a new lower case string copy.
- charAt ( int pos )
character at pos.
- trim( )
makes copy, white space removed from both ends.
None of these, however, can change the string on which a method is called. Remember toUpperCase.
If you want a string trimmed, here's what to do :
str = str.trim ( ) ;