Home arrow Java & J2EE arrow Page 5 - The JSP Files (part 3): Black Light And White Rabbits

Paying The Piper - Java

This week, learn all about the different types of loops supportedby JSP, and also expand your knowledge of the various String objectmethods. Finally, take a quick tour of the JSP Response object inpreparation for learning how JSP handles form data.

TABLE OF CONTENTS:
  1. The JSP Files (part 3): Black Light And White Rabbits
  2. Doing More With Loops
  3. For-gone Conclusion
  4. The Sound Of Breaking Loops
  5. Paying The Piper
  6. You Say Seven, I Say 7
  7. A Positive Response
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 2
March 01, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.{mospagebreak title=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


 
 
>>> More Java & J2EE Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: