SunQuest
 
       Java
  Home arrow Java arrow Page 5 - The JSP Files (part 3): Black Light An...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

The JSP Files (part 3): Black Light And White Rabbits
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2001-03-01

    Table of Contents:
  • The JSP Files (part 3): Black Light And White Rabbits
  • Doing More With Loops
  • For-gone Conclusion
  • The Sound Of Breaking Loops
  • Paying The Piper
  • You Say Seven, I Say 7
  • A Positive Response

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    The JSP Files (part 3): Black Light And White Rabbits - Paying The Piper


    (Page 5 of 7 )

    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 Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway