Previous Topic: &STR

Next Topic: &SOCKET Verbs


&SUBSTR

The &SUBSTR built-in function is used to extract data from within a string. If the data is single byte data only, this is a simple function. When DBCS strings are included, the standard &SUBSTR function can cause problems if the data within the part of the string which is defined by the offset and length operands contains DBCS data. The data that is extracted can contain shift characters that do not occur in shift out/shift in pairs. This can lead to problems, such as syntax errors, in later processing.

With &CONTROL DBCS, DBCSN, or DBCSP in effect, the offset and length is subject to variation due to the contents of the data. For example, if the original string contains shift characters, and &CONTROL DBCSN is in effect, the shift characters are not included in the calculation of the offset or length of the data that is to be extracted. If additional shift characters need to be added to the resultant string to preserve the integrity of the DBCS data, and they are included in the length of the string, the string could be truncated to ensure that the length of the extracted data is not greater than the length requested.

If the data at the specified offset is the second byte of a DBCS character, the character is replaced by a shift out character if shift characters are included in the string length, or by a single byte blank and a shift out character if shift characters are not included in the string length. This ensures that the following DBCS character extracted remains at the same logical offset in the resultant string.

If the data at the end of the extracted string is the first byte of a DBCS character, the character is replaced by a shift in character if shift characters are included in the string length, or by a shift in character and a single byte blank if shift characters are not included in the string length.

If the extracted data begins with a DBCS character, a shift out character is added to the front of the resultant string, to preserve the integrity of the DBCS string. If the extracted data ends with a DBCS character, a shift in character is added to the end of the resultant string, to preserve the integrity of the DBCS string.

If the original data string is shorter that the sum of the offset and length operands, no blank padding is added to the end of the resultant data string. However, a shift in is added if necessary.

If the data to be extracted is part of a DBCS string and the length specified is not large enough to allow a whole DBCS character to be extracted, the target variable is set to null.

Examples: &SUBSTR

&CONTROL DBCS 
&A = &SUBSTR AA<.A.B.C.D> 1 5

results in

&A = AA<>_

Note: The data was truncated to ensure that the final data length did not exceed the requested length. The shift characters are included in the length of the string. The resultant data has one trailing blank.

&CONTROL DBCSN 
&A = &SUBSTR AA<.A.B.C.D> 1 5

results in

&A = AA<.A>_

Note: The data would have ended on the first byte of a DBCS character (.B) which would be invalid, so the data has been truncated to remove the first byte of this character. The shift characters are not counted in the length of the string. The resultant data has one trailing blank.

&CONTROL DBCS 
&A = &SUBSTR AA<.A.B.C.D> 4 5

results in

&A = _<.B>

Note: The shift characters are counted in the offset and length of the string. A single blank has been added to the start of the data because the offset specified was the first byte of a DBCS character. Because of the requirement to add a shift in character, the data has been truncated.

&CONTROL DBCSN 
&A = &SUBSTR AA<.A.B.C.D> 5 2

results in

&A = <.B>
&A = &SUBSTR AA<.A.B.C.D> 5 4

results in

&A = <.B.C>

Note: The shift characters are not counted in the offset and length of the string. No data truncation is required, because the shift characters are not included in the length of the string.

&CONTROL DBCS 
&A = &SUBSTR AA<.A.B.C.D> 6 1

results in &A being null.

&A = &SUBSTR AA<.A.B.C.D> 6 2

results in &A being null.

In both cases, the data being extracted is from a DBCS string, and the length specified is not large enough for a DBCS character, so the target variable is set to null.