Tag library reference for the following tag libraries:
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.
Required attributes are marked with a *
Capitalize a String. The first character is converted to TitleCase.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
Will output: "Some Form Of Lower Case Sentence"
<str:capitalizeAllWords>some form of lower case sentence</str:capitalizeAllWords>
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Center the body String in a larger String of size width.
Can contain: JSP
Outputs a header with 'The Title' embedded in a bunch of - characters.
<str:center width="40" delimiter="-">The Title</str:center>
| 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 |
Remove the last occurence of a specified character, and everything after it.
Can contain: JSP
One use could be to remove # and // comments from code when outputted.
<str:chomp delimiter="#">Some code; # A comment</str:chomp>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to remove after. Default is a newline character. | String |
| var | Variable to store result in. | String |
Remove the last character from a String. If it is a \n and a \r precedes it, then remove that too.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
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>
| Name | Description | Type |
|---|---|---|
| set* | Character Set to look for. | String |
| var | Variable to store result in. | String |
Return the number of times that the body String contains the sub-string.
Can contain: JSP
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>
| Name | Description | Type |
|---|---|---|
| substring* | Substring to count number of. | String |
| var | Variable to store result in. | String |
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
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>
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
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>
| 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 |
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
Will output: 'Som smpl txt to dlt rom'
<str:delete set="aef">Some sample text to delete from</str:delete>
| Name | Description | Type |
|---|---|---|
| set* | Character Set to look for. | String |
| var | Variable to store result in. | String |
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
Especially useful if database output is being put into an <a href="..">
<str:encodeUrl>http://www.apache.org/some file//page.html</str:encodeUrl>
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Escape a String into a Java-compatible String. Handles unicode, and the standard escapes: \t, \b, \n, \f, \r, \\, \", \'
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Get everything after a specified delimiter. Returns the delimiter as well.
Can contain: JSP
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>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to get text after. Default is a newline character. | String |
| var | Variable to store result in. | String |
Get everything up until a specific delimiter. Returns the delimiter as well.
Can contain: JSP
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>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to get text before. Default is a newline character. | String |
| var | Variable to store result in. | String |
Joins the elements of the provided array.
Can contain: EMPTY
Useful to join elements of a list.
<% String[] users = new String[] { "john", "bob", "alice", "felipe" }; %>
<str:join items="<%=users%>" separator=" - "/>
| Name | Description | Type |
|---|---|---|
| items* | Array of elements to be joined. | String |
| separator | Separator string to be used. | String |
Get the leftmost 'n' characters from a string.
Can contain: JSP
Will output: '<A long piece of>'
<<str:left count="15">A long piece of text which will get cut.</str:rightPad>
| Name | Description | Type |
|---|---|---|
| count* | Number of characters to get. | String |
| var | Variable to store result in. | String |
Left pad a string with a particular delimiter to a specified width. The width includes the size of the string.
Can contain: JSP
Will output: '<-------An arrow>'
<<str:leftPad width="15" delimiter="-">An arrow</str:rightPad>
| 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 |
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
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Turn all uppercase and titlecase characters to lowercase.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Get the metaphone for a String. This is an improved variant of SoundEx.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
Will output: '<text which>'
<<str:mid count="10">A long piece of text which will get cut.</str:rightPad>
| Name | Description | Type |
|---|---|---|
| count* | Number of characters to get. | String |
| start | Position to start at. | String |
| var | Variable to store result in. | String |
Get a String that is nested between an open and closing substring.
Can contain: JSP
Pluck's 'Bob' from out of the square brackets.
<str:nestedString open="[" close="]">[Bob]</str:nestedString>
| 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 |
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
'pond' will be replaced by 'tree'
<str:overlay with="tree" start="4" end="8">The pond was wet.</str:overlay>
| 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 |
Remove everything up until a specified delimiter, and that specified delimiter from the start of a String.
Can contain: JSP
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>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to remove before. Default is a newline character. | String |
| var | Variable to store result in. | String |
Quote a String so that it will pass through a regular expression as a constant.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
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"/>
| 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 |
Remove any XML tags from a String. A very simple algorithm which just removes any <..> blocks of text.
Can contain: JSP
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>
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Repeat a string a particular number of times.
Can contain: JSP
Will output 'xxxxx'
<str:repeat count="5">x</str:repeat>
| Name | Description | Type |
|---|---|---|
| count* | Number of times to repeat the body. | String |
| var | Variable to store result in. | String |
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
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="<br>
">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="<br>NL" newlineToken="NL">
A sentence.
Another sentence.
Some more to test. And insert Br's into.
Hopefully.</str:replace>
| 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 |
Reverse a String.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
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>
| 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 |
Get the rightmost 'n' characters from a string.
Can contain: JSP
Will output: '<will get cut.>'
<<str:right count="13">A long piece of text which will get cut.</str:rightPad>
| Name | Description | Type |
|---|---|---|
| count* | Number of characters to get. | String |
| var | Variable to store result in. | String |
Right pad a string with a particular delimiter to a specified width. The width includes the size of the string.
Can contain: JSP
Will output: 'An arrow------->'
<str:rightPad width="15" delimiter="-">An arrow</str:rightPad>>
| 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 |
Return the US_ENGLISH Soundex of a String.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
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>
| Name | Description | Type |
|---|---|---|
| separator* | Separator string to be used. | String |
| var* | Variable to store result in. | String |
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
Will output: 'Some sample text to squeeze'
<str:squeeze set="xo">Some sample texxxt toooo squeeze</str:squeeze>
| Name | Description | Type |
|---|---|---|
| set* | Character Set to look for. | String |
| var | Variable to store result in. | String |
Strip all of a specified character which appear at the start and end of a String.
Can contain: JSP
Default is isWhitespace, that is tab or newline or space character.
<str:strip delimiter="-">---A Title---</str:strip>
| 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 |
Strip all of a specified character which appear at the end of a String.
Can contain: JSP
<str:stripEnd delimiter="-">A Title---</str:stripEnd>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to remove from end of body. Default is a isWhitespace() character. | String |
| var | Variable to store result in. | String |
Strip all of a specified character which appear at the start of a String.
Can contain: JSP
<str:stripStart delimiter="-">---A Title</str:stripStart>
| Name | Description | Type |
|---|---|---|
| delimiter | Character to remove from start of body. Default is a isWhitespace() character. | String |
| var | Variable to store result in. | String |
Get a specified substring from a larger String based on the start index and end-before index in the larger String
Can contain: JSP
Will get the substring 'tree' from the body.
<str:substring start="4" end="8">The tree is green.</str:substring>
| 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 |
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
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
Remove whitespace from start and end of a String.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
Will output: '<The quick brown...>'
<<str:truncateNicely lower="13" upper="20">The quick brown fox jumped over the lazy dog.</str:truncateNicely>
| 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 |
Change the first letter of a String to Lowercase.
Can contain: JSP
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
| Name | Description | Type |
|---|---|---|
| var | Variable to store result in. | String |
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
<str:wordWrap width ="50" delimiter="<br>">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>
| 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 |