Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Paying The Piper

You've already seen a few of the String object's capabilities in the first part of this tutorial. But as we progress further, you're going to need to know a little bit more to make full use of its power.

First, there's the indexOf() method, which is used to locate the first occurrence of a character or substring in a larger string. If you ran the following code snippet,

<%            
// define variable            
String storyName = "The Pied Piper Of Hamlin";            
           
// find index            
int i = storyName.indexOf("i");            
           
// print index            
out.println("The letter i first occurs at " + i + "              
in the string " + storyName); %>

this is what you would see:

The letter i first occurs at 5 in the string The              
Pied Piper Of Hamlin

Yes, the first character is treated as index 0, the second as index 1, and so on. These programmers...

The opposite of this is the lastIndexOf() function, used to identify the last occurrence of a character or substring in a larger string. Take a look:

<%            
// define variable            
String storyName = "The Pied Piper Of Hamlin";            
           
// find index            
int i = storyName.lastIndexOf("Pi");            
           
// print index            
out.println("The string Pi last occurs at " + i + "              
in the string " + storyName); %>

And the output is:

The string Pi last occurs at 9 in the string The              
Pied Piper Of Hamlin

In case the character or substring is not located, the function will return an error code of -1.

Screaming Out Loud

Next, the trim() function comes in handy when you need to remove white space from the ends of a string.

<%            
// define variable            
String whatIWant = " gimme my space ";            
           
// trim!            
// returns "gimme my space"            
whatIWant.trim();            
%>

The toUpperCase() and toLowerCase() methods come in handy to alter the case of a string.

<%            
// define variable            
String someString = "don't SCREam, help is oN the WAy!";            
           
// uppercase - returns "DON'T SCREAM, HELP IS ON THE WAY!"              
someString.toUpperCase();            
           
// lowercase - returns "don't scream, help is on the way!"              
someString.toLowerCase(); %>

The startsWith() and endsWith() functions are used to verify whether a string starts or ends with a specified character or sequence of characters. The following example should illustrate this clearly.

<%            
// define variables            
String alpha = "black light";            
String beta = "white rabbit";            
String prefix = "bl";            
String suffix = "it";            
           
// check each string for prefixes and suffixes            
           
if (alpha.startsWith(prefix))            
{            
out.println("The string " + alpha + " starts with              
" + prefix + "<br>"); }            
           
if (beta.startsWith(prefix))            
{            
out.println("The string " + beta + " starts with              
" + prefix + "<br>"); }            
           
if (alpha.endsWith(suffix))            
{            
out.println("The string " + alpha + " ends with " +              
suffix + "<br>"); }            
           
if (beta.endsWith(suffix))            
{            
out.println("The string " + beta + " ends with " +              
suffix + "<br>"); }            
           
%>

And the output is:

The string black light starts with bl            
The string white rabbit ends with it

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article