Working with Text (Strings)

Modified on Mon, Dec 4, 2023 at 10:37 AM

Strings in Expressions


Working with Text: String Function and Facilities


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

 

Comparing Text

 

Text can be compared directly if the two Strings are supposed to exactly match:

              ${  row.lastName == params.findThisName ?  ‘Matches’ : ‘Does not Match’ }

 

If you are looking for text that only partially matches, several Boolean 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:containsh(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}
    • E.g. rw:endsWith(row.courseCode, "E") - returns true if the course code ends with an "E". Returns false otherwise.

 

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: rw:concat(row.string1,row.string2) This concatenates the two strings into a single string: no separators are added. 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)


Please look at the detailed example in the Concatenation: Putting Multiple Strings Together article.

 

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
    • ${rw:replace(row.lunchCode,'F','Free')}
    • ${rw:replace(row.lunchCode,'R','Reduced')}
    • ${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)
    • ${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
    • ${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. So only the one that does 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