2010-04-14 - Jakarta Taglibs has been retired.

For more information, please explore the Attic.

Tag reference sheet

Tag library reference for the following tag libraries:

String Tag library

The String taglibrary provides a host of tags for manipulating java.lang.Strings. The style is that the String to act upon is the body of the tag, and attributes are used as parameters for the manipulation.

This is version 1.1.0.

  • capitalize Capitalize a String
  • capitalizeAllWords Capitalize all the words found in a piece of body text
  • center Center the body String in a larger String of size width
  • chomp Remove the last occurence of a specified character, and everything after it
  • chop Remove the last character from a String
  • chopNewline Remove a \n from the end of a String if it is there
  • count Count the number of occurences of a passed in 'Set'
  • countMatches Return the number of times that the body String contains the sub-string
  • decodeUrl Decode a URL so it may be nicely printed
  • default If the body String is a particular value, then output a default value, else output the body String
  • delete Delete any characters from the body that match characters specified in the Set
  • encodeUrl Encode a String to that it may be used as a URL
  • escape Escape a String into a Java-compatible String
  • getChomp Get everything after a specified delimiter
  • getPrechomp Get everything up until a specific delimiter
  • join Joins the elements of the provided array
  • left Get the leftmost 'n' characters from a string
  • leftPad Left pad a string with a particular delimiter to a specified width
  • length Return the length of a String
  • lowerCase Turn all uppercase and titlecase characters to lowercase
  • metaphone Get the metaphone for a String
  • mid Get 'n' characters from the inside of a string
  • nestedString Get a String that is nested between an open and closing substring
  • overlay Overlay a String on top of another String
  • prechomp Remove everything up until a specified delimiter, and that specified delimiter from the start of a String
  • quoteRegexp Quote a String so that it will pass through a regular expression as a constant
  • randomString Create a random string
  • removeXml Remove any XML tags from a String
  • repeat Repeat a string a particular number of times
  • replace Replace a specified substring with another string
  • reverse Reverse a String
  • reverseDelimitedString Reverses a delimited String
  • right Get the rightmost 'n' characters from a string
  • rightPad Right pad a string with a particular delimiter to a specified width
  • soundex Return the US_ENGLISH Soundex of a String
  • split Splits the provided text into an array, separators specified
  • squeeze Squeeze any characters from the body that match characters specified in the Set
  • strip Strip all of a specified character which appear at the start and end of a String
  • stripEnd Strip all of a specified character which appear at the end of a String
  • stripStart Strip all of a specified character which appear at the start of a String
  • substring Get a specified substring from a larger String based on the start index and end-before index in the larger String
  • swapCase Swap the casing in a String
  • trim Remove whitespace from start and end of a String
  • truncateNicely A more intelligent substring
  • uncapitalize Change the first letter of a String to Lowercase
  • upperCase Turn the entire String to uppercase
  • wordWrap Word-wrap a String

Required attributes are marked with a *

<string:capitalize>

Capitalize a String. The first character is converted to TitleCase.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:capitalizeAllWords>

Capitalize all the words found in a piece of body text. Capitalization is performed upon any letters following whitespace, and on the first character.

Can contain: JSP

Example

Will output: "Some Form Of Lower Case Sentence"
 
<str:capitalizeAllWords>some form of lower case sentence</str:capitalizeAllWords>
       
       

Attributes

Name Description Type
var Variable to store result in. String

<string:center>

Center the body String in a larger String of size width.

Can contain: JSP

Example

Outputs a header with 'The Title' embedded in a bunch of - characters.
 
<str:center width="40" delimiter="-">The Title</str:center>
       
        

Attributes

Name Description Type
delimiter String to pad the larger String with. Default is a space character. String
var Variable to store result in. String
width* Size of larger String. String

<string:chomp>

Remove the last occurence of a specified character, and everything after it.

Can contain: JSP

Example

One use could be to remove # and // comments from code when outputted.
 
<str:chomp delimiter="#">Some code; # A comment</str:chomp>
       
        

Attributes

Name Description Type
delimiter Character to remove after. Default is a newline character. String
var Variable to store result in. String

<string:chop>

Remove the last character from a String. If it is a \n and a \r precedes it, then remove that too.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:chopNewline>

Remove a \n from the end of a String if it is there. If a \r precedes it, then it removes that too.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:count>

Count the number of occurences of a passed in 'Set'. A Set is a String of a particular format. It may include ranges and single characters. A Set may be negated. null

Can contain: JSP

Example

There is 1 a, 2 e's and no f's. So the result is 3.
 
<str:count set="aef">Some sample text to count</count>
       
        

Attributes

Name Description Type
set* Character Set to look for. String
var Variable to store result in. String

<string:countMatches>

Return the number of times that the body String contains the sub-string.

Can contain: JSP

Example

The substring 'world' is repeated four times in the body.
 
<str:countMatches substring="world">Hello world programs are rife in this world due to their worldy view of how to program a world of code</str:countMatches>
       
        

Attributes

Name Description Type
substring* Substring to count number of. String
var Variable to store result in. String

<string:decodeUrl>

Decode a URL so it may be nicely printed. null accented characters may not be encoded correctly, but there is not we can do about it (a solution would be passing the encoding as an attribute, but that would require a J2SE 1.4 environment).

Can contain: JSP

Example

Useful in parallel with its counterpart. If a database contains the URL in 
encoded form, then this could be used to show a human readable version.
 
<str:decodeUrl>http://www.apache.org/some+file%20/page.html</str:decodeUrl>
       
        

Attributes

Name Description Type
var Variable to store result in. String

<string:default>

If the body String is a particular value, then output a default value, else output the body String. The major use of this is to convert 'null' into an empty String.

Can contain: JSP

Example

If the inner tag (db:get) return null (or the string "null") then an empty 
string will be printed out.
 
<str:default><db:get value="Name"></str:default>
       
        

Attributes

Name Description Type
default Value to place if body is equal to value field. Default is an empty String. String
value Value to be equal to. Default is the string: "null". String
var Variable to store result in. String

<string:delete>

Delete any characters from the body that match characters specified in the Set. The syntax of a Set is described in Count.

Can contain: JSP

Example

Will output: 'Som smpl txt to dlt rom'
 
<str:delete set="aef">Some sample text to delete from</str:delete>
       
        

Attributes

Name Description Type
set* Character Set to look for. String
var Variable to store result in. String

<string:encodeUrl>

Encode a String to that it may be used as a URL. null accented characters may not be encoded correctly, but there is not we can do about it (a solution would be passing the encoding as an attribute, but that would require a J2SE 1.4 environment).

Can contain: JSP

Example

Especially useful if database output is being put into an <a href="..">
 
<str:encodeUrl>http://www.apache.org/some file//page.html</str:encodeUrl>
       
        

Attributes

Name Description Type
var Variable to store result in. String

<string:escape>

Escape a String into a Java-compatible String. Handles unicode, and the standard escapes: \t, \b, \n, \f, \r, \\, \", \'

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:getChomp>

Get everything after a specified delimiter. Returns the delimiter as well.

Can contain: JSP

Example

Similarily to its counterpart, this could be used to get the comments from a 
piece of code.
 
<str:getChomp delimiter="#">Some code; # A comment</str:getChomp>
       
        

Attributes

Name Description Type
delimiter Character to get text after. Default is a newline character. String
var Variable to store result in. String

<string:getPrechomp>

Get everything up until a specific delimiter. Returns the delimiter as well.

Can contain: JSP

Example

In the code comment theme, this would return the code but include the comment 
character itself.
 
<str:getPrechomp delimiter="#">Some code; # A comment</str:getPrechomp>
       
        

Attributes

Name Description Type
delimiter Character to get text before. Default is a newline character. String
var Variable to store result in. String

<string:join>

Joins the elements of the provided array.

Can contain: EMPTY

Example

Useful to join elements of a list.

<% String[] users = new String[] { "john", "bob", "alice", "felipe" }; %>
<str:join items="<%=users%>" separator=" - "/>
       
        

Attributes

Name Description Type
items* Array of elements to be joined. String
separator Separator string to be used. String

<string:left>

Get the leftmost 'n' characters from a string.

Can contain: JSP

Example

Will output:  '<A long piece of>'
 
&lt;<str:left count="15">A long piece of text which will get cut.</str:rightPad>
       
        

Attributes

Name Description Type
count* Number of characters to get. String
var Variable to store result in. String

<string:leftPad>

Left pad a string with a particular delimiter to a specified width. The width includes the size of the string.

Can contain: JSP

Example

Will output:  '<-------An arrow>'
 
&lt;<str:leftPad width="15" delimiter="-">An arrow</str:rightPad>
       
        

Attributes

Name Description Type
delimiter String to pad the larger String with. Default is a space character. String
var Variable to store result in. String
width* Size of larger String. String

<string:length>

Return the length of a String. An empty string is returned for a null String if one should somehow be passed in.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:lowerCase>

Turn all uppercase and titlecase characters to lowercase.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:metaphone>

Get the metaphone for a String. This is an improved variant of SoundEx.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:mid>

Get 'n' characters from the inside of a string. The start position may be set, or the default may be used. The default is to get the central characters.

Can contain: JSP

Example

Will output:  '<text which>'
 
&lt;<str:mid count="10">A long piece of text which will get cut.</str:rightPad>
       
        

Attributes

Name Description Type
count* Number of characters to get. String
start Position to start at. String
var Variable to store result in. String

<string:nestedString>

Get a String that is nested between an open and closing substring.

Can contain: JSP

Example

Pluck's 'Bob' from out of the square brackets.
 
<str:nestedString open="[" close="]">[Bob]</str:nestedString>
       
        

Attributes

Name Description Type
close Index to stop substring before. Default is the end of the string. String
open* Index to start substring at. String
var Variable to store result in. String

<string:overlay>

Overlay a String on top of another String. The length of the String to overlay does not have to be the same as the size of substring it replaces.

Can contain: JSP

Example

'pond' will be replaced by 'tree'
 
<str:overlay with="tree" start="4" end="8">The pond was wet.</str:overlay>
       
        

Attributes

Name Description Type
end* Index to end before. String
start* Start index. String
var Variable to store result in. String
with* String to overlay onto the body. String

<string:prechomp>

Remove everything up until a specified delimiter, and that specified delimiter from the start of a String.

Can contain: JSP

Example

Again, this could be used to get the comment from a piece of code. However it 
would not include the delimiter as getChomp would.
 
<str:prechomp delimiter="#">Some code; # A comment</str:prechomp>
       
        

Attributes

Name Description Type
delimiter Character to remove before. Default is a newline character. String
var Variable to store result in. String

<string:quoteRegexp>

Quote a String so that it will pass through a regular expression as a constant.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:randomString>

Create a random string. It can create an ascii string, a numeric string, an alphanumeric string, and a unicode string. The size of the string may be specified, as can a start and end character. Lastly, a set of characters to use may be passed in.

Can contain: JSP

Example

The random tag has a lot of options. The easiest way is to just use the type 
field, as in the example. The second example shows how the developer can 
specify the characters to use, for example, random vowels. The last example 
is merely demonstrative, no one would want to do this, but it shows how the 
developer may specify the range of characters, in this example, all the 
control characters.
 
<str:random count="10" type="alphanumeric"/>
<str:random count="1">aeiou</str:random>
<str:random count="5" start="1" end="32"/>
       
        

Attributes

Name Description Type
count* Number of characters to generate. String
end End of range of characters to generate from. Linked to type attribute. String
start Start of range of characters to generate from. Linked to type attribute. String
type Type of random-string. One of: numeric | alphanumeric | alphabet | unicode. Default value is to unicode. String
var Variable to store result in. String

<string:removeXml>

Remove any XML tags from a String. A very simple algorithm which just removes any <..> blocks of text.

Can contain: JSP

Example

All it currently does is strip any <xx> tags out. It doesn't check that 
the tags are correct xml and it would be confused by < or > signs 
inside a tag attribute.
 
<str:removeXml><name>John Brown</name></str:removeXml>
       
        

Attributes

Name Description Type
var Variable to store result in. String

<string:repeat>

Repeat a string a particular number of times.

Can contain: JSP

Example

Will output 'xxxxx'
 
<str:repeat count="5">x</str:repeat>
       
        

Attributes

Name Description Type
count* Number of times to repeat the body. String
var Variable to store result in. String

<string:replace>

Replace a specified substring with another string. A number of times may be specified to say how many times to replace the substring.

Can contain: JSP

Example

One use of the replace tag is to change newlines to html br's. Note that real 
newlines need to be entered into the JSP.
 
<str:replace replace="
" with="&lt;br&gt;
">A sentence.
Another sentence.
Some more to test. And insert Br's into.
Hopefully.</str:replace>
       
_or_ with 1.0.1:
 
<str:replace replace="NL" with="&lt;br&gt;NL" newlineToken="NL">
A sentence.
Another sentence.
Some more to test. And insert Br's into.
Hopefully.</str:replace>
       
        

Attributes

Name Description Type
carriageToken Token to use instead of a carriage return to get around JSP pain. Default is to ignore this functionality. String
count Number of times to replace. Default is all matches. String
newlineToken Token to use instead of a newline to get around JSP pain. Default is to ignore this functionality. String
replace* Value to find to replace. String
var Variable to store result in. String
with* Value to replace with. String

<string:reverse>

Reverse a String.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:reverseDelimitedString>

Reverses a delimited String. One example would be changing org.apache.taglib to taglib.apache.org. The delimiter to use is settable.

Can contain: JSP

Example

Major use is to turn domain names into java package names. Bound to be other uses.
 
<str:reverseDelimitedString delimiter=".">org.apache.taglib.string.String</str:reverseDelimitedString>
       
        

Attributes

Name Description Type
delimiter Character to use as the delimiter in the qualified name. Default is a dot character. char
var Variable to store result in. String

<string:right>

Get the rightmost 'n' characters from a string.

Can contain: JSP

Example

Will output:  '<will get cut.>'
 
&lt;<str:right count="13">A long piece of text which will get cut.</str:rightPad>
       
        

Attributes

Name Description Type
count* Number of characters to get. String
var Variable to store result in. String

<string:rightPad>

Right pad a string with a particular delimiter to a specified width. The width includes the size of the string.

Can contain: JSP

Example

Will output:  'An arrow------->'
 
<str:rightPad width="15" delimiter="-">An arrow</str:rightPad>&gt;
       
        

Attributes

Name Description Type
delimiter String to pad the larger String with. Default is a space character. String
var Variable to store result in. String
width* Size of larger String. String

<string:soundex>

Return the US_ENGLISH Soundex of a String.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:split>

Splits the provided text into an array, separators specified. The separator is not included in the returned String array and adjacent separators are treated as one separator.

Can contain: JSP

Example

This is the reverse of join and is the equivalent of StringTokenizer in a tag.

<%
<str:split separator=" - " var="users">john - bob - alice - felipe</str:split>
       
        

Attributes

Name Description Type
separator* Separator string to be used. String
var* Variable to store result in. String

<string:squeeze>

Squeeze any characters from the body that match characters specified in the Set. Squeezing means that if it finds two adjoining characters that are the same, they are replaced with a single one of the characters. The syntax of a Set is described in the Count tag.

Can contain: JSP

Example

Will output: 'Some sample text to squeeze'
 
<str:squeeze set="xo">Some sample texxxt toooo squeeze</str:squeeze>
       
        

Attributes

Name Description Type
set* Character Set to look for. String
var Variable to store result in. String

<string:strip>

Strip all of a specified character which appear at the start and end of a String.

Can contain: JSP

Example

Default is isWhitespace, that is tab or newline or space character.
 
<str:strip delimiter="-">---A Title---</str:strip>
       
        

Attributes

Name Description Type
delimiter Character to remove from start and end of body. Default is a isWhitespace() character. String
var Variable to store result in. String

<string:stripEnd>

Strip all of a specified character which appear at the end of a String.

Can contain: JSP

Example

 
<str:stripEnd delimiter="-">A Title---</str:stripEnd>
       
        

Attributes

Name Description Type
delimiter Character to remove from end of body. Default is a isWhitespace() character. String
var Variable to store result in. String

<string:stripStart>

Strip all of a specified character which appear at the start of a String.

Can contain: JSP

Example

 
<str:stripStart delimiter="-">---A Title</str:stripStart>
       
        

Attributes

Name Description Type
delimiter Character to remove from start of body. Default is a isWhitespace() character. String
var Variable to store result in. String

<string:substring>

Get a specified substring from a larger String based on the start index and end-before index in the larger String

Can contain: JSP

Example

Will get the substring 'tree' from the body.
 
<str:substring start="4" end="8">The tree is green.</str:substring>
       
        

Attributes

Name Description Type
end Index to end before. May be negative, which means it is the index counting backwards from the end of the string. May also be longer than the length of the string, in which case it quietly sets it to the length of the string. String
start Start index. May be negative, which means it is the index counting backwards from the end of the string. Default is 0. String
var Variable to store result in. String

<string:swapCase>

Swap the casing in a String. Lowercase goes to Uppercase or Titlecase depending on whether they are the first in the String or after whitespace. Uppercase and Titlecase go to Lowercase

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:trim>

Remove whitespace from start and end of a String.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:truncateNicely>

A more intelligent substring. It attempts to cut off a string after a space, following predefined or user-supplied lower and upper limits, useful for making short descriptions from long text. It strips the HTML out as a part of the code. Patches on a version which doesn't strip the HTML will be gratefully received.

Can contain: JSP

Example

Will output:  '<The quick brown...>'
 
&lt;<str:truncateNicely lower="13" upper="20">The quick brown fox jumped over the lazy dog.</str:truncateNicely>
       
        

Attributes

Name Description Type
appendToEnd String to append to the end of the truncated string. String
lower Mininum length to truncate at. String
upper Maximum length the string can be. Will force a truncate at this point. String
var Variable to store result in. String

<string:uncapitalize>

Change the first letter of a String to Lowercase.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:upperCase>

Turn the entire String to uppercase. Delegates to java.lang.String, so no idea if it uses Titlecase when it's supposed to.

Can contain: JSP

Attributes

Name Description Type
var Variable to store result in. String

<string:wordWrap>

Word-wrap a String. This involves formatting a long String to fit within a certain character width of page. A delimiter may be passed in to put at the end of each line and a splitting character can be specified for when a word has to be cut in half.

Can contain: JSP

Example

 
<str:wordWrap width ="50" delimiter="&lt;br&gt;">Word-wrap a String. This involves formatting a long String to fit within a certain character width of page. A delimiter may be passed in to put at the end of each line and a splitting character can be specified for when a word has to be cut in half.</str:wordWrap>
       
        

Attributes

Name Description Type
delimiter Character to put between each line. Default is a newline character. String
delimiterInside Flag indicating if the delimiter should be included in chunk before length reaches width. Default is true. boolean
split Character to use when a word has to be split. Default is a - character. String
var Variable to store result in. String
width Width to word wrap to. Default is 80. String