Work with Text (Strings) in Reports

Modified on Thu, Nov 30, 2023 at 2:34 PM

Strings in Expressions


Text is called a "String" and working with "Strings" is how to combine and alter text in Expressions.


Comparing Text


Several functions are provided to help you compare short pieces of text:

  • Contains - Returns true if the first string contains the second string. Returns false if the second string is not found in the first string: rw.contains (string,comparator) E.g. rw:contains(row.courseCode,"701") -- Returns true if the course code contains "701". Returns false if it does not.
  • startsWith - Returns true if the first string starts with the second string. Returns false otherwise: rw:startsWith(string,comparator) E.g. rw.startsWith(row.homeroom, "2") - Returns true if the homeroom begins with a "2" and returns false otherwise.
  • endsWith - Returns true if the first string ends with the second string, returns false otherwise: rw:endswith(string,comparator). rw:endsWith(row.courseCode, "E") - returns true if the course code ends with an "E". Returns false otherwise.

 These functions can appear with the ?: conditional operator in order to make decisions.


Concatenation

 

Concatenation - There are several ways to concatenate Strings into a single longer String. Strings in multiple, separate Columns can be concatenated together in a single Expression column simply by including the Column references in the overall Expression text for the combined column. To concatenate columns into a single ‘string’, simply place each row-variable separately on the line. Note that if something is optional – such as a name suffix (“Jr.”, “Sr.”, “III”, “IV”…), you may need to include two or repetitive conditional expressions to add everything you need:

  • E.g. concatenate separate name fields together: ‘${row.honorific} ${row.firstname} ${row.middleName} ${row.lastName}${empty row. suffix ? "" : ","} ${row.suffix}
  • In this example, the test '${empty row.suffix ? "" : ", "}' determines if the "suffix" Column is empty. If it is, an empty String "" is added. If suffix is not empty, a comma is added: ", "
  • The suffix '${row.suffix}' is then added at the end: if it is empty, it will print nothing. If there is a suffix, it will be added after the comman. E.g. ", Jr."
  • All the listed expressions are concatenated together simply by being listed in the Expression text.
  • Another use for concatenation would be to add City, State Zip address lines: ‘${row.homeaddressCity}, ${row.homeaddressState} ${row. homeaddressZipCode}’
  • CAVEAT: ALL TEXT OUTSIDE OF EXPRESSIONS (outside of "${" and "}" brackets) is literal text. It is printed exactly as it appears. This includes all spaces, commas and newlines. "Empty" space is not empty - it is filled with blank characters and perhaps newline characters. These "invisible" characters are not stripped off: they are printed out exactly as they are encountered in the Expression text.

 

There are also four specific concatenation functions:

  • Concatenate 2 Strings into a single string:
    1.  rw:concat(row.string1,row.string2) This concatenates the two strings into a single string: no separators are added.
    2. The two strings do not need to be (as they appear to be above) fields in the row. They could be report parameters or literals (the text itself, in quotes) rw:concat(row.field1,": ") rw:concat(params.UserQuestion,"?")
  • Concatenate 3 Strings into a single string: the same as the "rw:concat3" function but for three strings instead of two. rw:concat3(field1, field2, field3)
  • Concatenate 4 Strings into a single string: the same as the "rw:concat4" function but for four strings instead of two or three. rw:concat4(field1, field2, field3, field4)
  • Concatenate 5 Strings into a single string: the same as the "rw:concat5" function but for five strings instead of two, three or four. rw:concat5(field 1, field2, field3, field4, field)

 

String Functions

  • Trim "whitespace" from a String - rw:trim(params.usersanswer) This "trims" or removes any leading or trailing blanks from a String, along with any leading or trailing tabs or newline characters.
  • Convert to All Upper Case ${rw:toUpperCase(row.fullName)} This results in a String that is all in upper case
  • Convert to All Lower Case: ${rw:toUpperCase(row.fullName)} This results in a String that is all in lower case
  • InitCaps - Convert to All Lower Case and Capitalize the First Letter of Every Word: ${rw:initCaps(row.fullName)}
  • Replacing Text - This allows one bit of text to be replaced with other text
    1. ${rw:replace(row.lunchCode,'F','Free')}
    2. ${rw:replace(row.lunchCode,'R','Reduced')}
    3. ${rw:replace(row.booleanValue,'true','True')
  • Pulling a substring out of a longer String - This allows you to pull a known section out of a longer String: substring(String,start-position, end[1]position)
    1. ${rw:substring(row.phoneNumber,1,3)} - This pulls positions 1 to 3 out of a presumably 10-digit phone number: This is typically the Area Code.
  • Remove End of Line characters from a String- This removes all end of line characters from a String and converts a multi-line String into a single line
    1. ${rw:removeEols(row.comments)} - The "removeEols" function strips out all end of line characters from the String and returns the 'clean' String


Replacing Multiple Possible Strings with a Single Column - A ReportWriter 'Look Up Table' 

When it is desirable to replace different bits of text with other bits of text, there is a bit of trick to creating the equivalent of a ‘lookup table’. For example, suppose you wanted to replace “F” with “Free” and “R” with “Reduced” all in the same Column? 

The way to accomplish this is to create a list of expressions containing “replace” functions with no spaces between them in the Expression text:


   ${rw:replace(row.lunch,'F','Free')}${rw:replace(row.lunch,'R','Reduced')}${rw:replace(row.lunch,'D','Denied')}


The trick is to leave no spaces, no commas and no newlines between each of the replace functions. When one of the replace functions does not get evaluated, does not fire off, it is replaced by nothing – no spaces, no anything. Only the function that does do work fills the Column. Any literal space (and spaces, commas and newlines are literal text) is faithfully copied to the output.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article