Java
  Home arrow Java arrow Page 6 - The JSP Files (part 1): Purple Pigs In A Fruitbasket
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
JAVA

The JSP Files (part 1): Purple Pigs In A Fruitbasket
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2001-02-07


    Table of Contents:
  • The JSP Files (part 1): Purple Pigs In A Fruitbasket
  • Studying The Foundations
  • Java In A Teacup
  • Enter John Doe
  • Putting Two And Two Together
  • Basket Case
  • Alphabet Soup For The Soul

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    The JSP Files (part 1): Purple Pigs In A Fruitbasket - Basket Case
    ( Page 6 of 7 )

    The String object comes with a bunch of useful methods, which can come in handy when performing string manipulation.

    The first of these is the length() method, used to obtain the (you guessed it!) length of a specific string. Let's modify the example you just saw to demonstrate how this works:


    <html> <head> </head> <body> <%! // define the variables String apples = "Purple pigs "; String oranges = "riding orange pumpkins"; String fruitBasket; %> <% // print the first two strings out.println("<b>The first string is</b>: " + apples + "<br>"); out.println("<b>The second string is</b>: " + oranges + "<br>"); // concatentate the strings fruitBasket = apples + oranges; // display out.println("<b>And the combination is</b>: " + fruitBasket + "(" + fruitBasket.length() + " characters)<br>Who says you can't add apples and oranges?!"); %> </body> </html>

    And the output is:

    The first string is: Purple pigs The second string is: riding orange pumpkins And the combination is: Purple pigs riding orange pumpkins(34 characters) Who says you can't add apples and oranges?!

    You can extract a specific character from the string with the charAt() method, which accepts an offset as parameter. For example, the following code snippet would return the character "o":

    <% String name = "Bozo The Clown"; out.println(name.charAt(3)); %>

    Note that the offset 0 indicates the first character, since Java, like many of its counterparts, uses zero-based indexing.

    You can also extract a segment of a string with the substring() method, which allows you to specify the start and end points of the string segment to be extracted. Take a look at this sentence and see if you can spot the hidden message within it:

    <%! String me = "I am a highly-skilled and hardworking developer!"; %>

    No? How about now?

    <%! String me = "I am a highly-skilled and hardworking developer!"; String message; %> <% message = me.substring(0,2) + me.substring(15,22) + me.substring(26,27) + me.substring(45,48); out.println(message); %>

    And here's the output:

    I killed her!


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

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek