Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
The JSP Files (part 2): Attack Of The Killer Fortune Cookies
Conditional expressions, logical and comparsion operators, and a basket full of fortune cookies. What more could you ask for?

| Flavour Of The Month |

And just as you can compare numbers, JSP also allows you to compare strings, with a couple of very useful String object methods.

First, the equals() method allows you to check whether the value of a particular string variable matches another. The following example should demonstrate this.


<%
// define variables
String myFavourite = "chocolate";
String yourFavourite = "strawberry";

// compare strings
if (myFavourite.equals(yourFavourite))
{
out.println("A match made in heaven!");
}
else
{
out.println("Naw - try again!");
}
%>


Try changing the values of the variables to match each other, and gasp in awe as the output changes.

In case the equals() method doesn't appeal to you, JSP offers you a choice in the form of the compareTo() method, which returns a value indicating which of the two strings is greater. Take a look:


<%
// define variables
String alpha = "abcdef";
String beta = "zyxwvu";

// compare strings
out.println(alpha.compareTo(beta));
%>


In this case, if the value of the variable "beta" is greater than that of the variable "alpha", the compareTo() method will return a negative integer; if it's the other way around, the comparison will return a positive integer. And if the two strings are identical, the comparison will return 0.

Incidentally, the comparison is based on both the first character of the string, and the number of characters in the string. One string is considered "greater" than another if the numeric value of its first character is greater, or if its length is greater. In the example above, "z" has a greater numeric code than "a", and so the comparison will return a negative integer. But don't take our word for it - try it yourself and see!


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback