Writing Expressions : Manipulating string data : Getting parts of a string
 
Getting parts of a string
Sometimes, you want to display only a portion of a string. For example:
*An address field stores a full address, but you want to display only the zip code or the state.
*A name field stores a full name, and you want only the first or last name.
*An e-mail field stores e-mail addresses, and you want only the user name that precedes the @ symbol.
Depending on the content of the string and which part of a string you need—the first part, the last part, or a part after or before a particular character—the expression that you specify varies. The JavaScript functions you are likely to use in the expression include the functions shown in Table 11‑2.
Table 11‑2 Getting information about a string
Function
Use to
charAt( )
Get the character at the specified position of a string. Note that in JavaScript, the first character starts at 0, not 1.
indexOf( )
Find the first occurrence of a specified character and return its position in the original string.
lastIndexOf( )
Find the last occurrence of a specified character and return its position in the original string.
length
Get the length of a string. Note that length is a property of a string, not a function, so do not use parentheses, ( ), after the keyword, length.
substr( )
Return a substring of a specified length, starting from a particular position in the original string.
The following examples show how to get different parts of a string. Assume a customerName field stores names in first name and last name format, such as Robert Allen.
*To get the first name:
*Use indexOf( ) to get the position of the space character that separates the first name from the last name.
*Use substr( ) to get the first name, starting from the first character and for a specified length. The first character for JavaScript starts at 0, not 1. The length to specify is equal to the position of the space character, and not the position of the space character minus 1, as you might think. Consider the name Robert Allen. Logically, the space between the first and last names is the seventh character, but JavaScript counts its position as six. To return the first name, Robert, excluding the space, you want substr( ) to return six characters.
The following expression returns the first name:
spaceCharPosition = row["customerName"].indexOf(" ");
newStringtoDisplay = row["customerName"].substr(0, spaceCharPosition);
*To get the last name, use indexOf( ) and substr( ) again. The difference is the arguments that you specify for substr( ). To get the last name, start from the character after the space, and the number of characters that you want is the length of the entire string minus the length up to the space.
The following expression returns the last name:
spaceCharPosition = row["customerName"].indexOf(" ");
newStringtoDisplay = row["customerName"].substr(spaceCharPosition + 1, row["customerName"].length - spaceCharPosition);
*To get the first name initial and the last name, for example, R. Allen, to display in the report:
*Use the expression in the previous example to get the last name.
*Add a statement that gets the first letter in the customerName field. You can use substr(0,1) to get only the first character. Alternatively, use charAt(0), which returns a character in a specified position of a string.
*Add a statement to combine the first name initial, a period, a space, and the last name.
The following expression returns the first name initial and last name:
firstNameInitial = row["customerName"].charAt(0);
spaceCharPosition = row["customerName"].indexOf(" ");
lastName = row["customerName"].substr(spaceCharPosition + 1, row["customerName"].length - spaceCharPosition);
newStringtoDisplay = firstNameInitial + ". " + lastName;