String

From VSI OpenVMS Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

A string is a data type in OpenVMS that represents a sequence of ASCII characters. Here is a symbol that has a string type:

$ TEST == "This is a string symbol"
$ SHOW SYMBOL TEST
  TEST == "This is a string symbol"
 

To label a sequence of characters as a string, put it in double quotes.

String Operations

The following string operations are possible:

  • string concatenation:
$ a = "This is a "
$ b = "string"
$ c = a + b
$ show sym c
  C = "This is a string"
 
  • string subtraction (works for the first substring located inside of the string):
3$ d = c-"a"
SMAN43$ show sym d
  D = "This is  string"
 
  • locating substrings in a string (with F$LOCATE()):
$ WRITE SYS$OUTPUT F$LOCATE("E","HELLO")
1
 
$ WRITE SYS$OUTPUT F$LENGTH("HELLO")
5
 
  • extracting a substring from a string (with F$EXTRACT()):
$ WRITE SYS$OUTPUT F$EXTRACT("1",2,"HELLO")
EL
 
  • extracting an element from a string with delimiters (with F$ELEMENT()):
$ WRITE SYS$OUTPUT F$ELEMENT(0,"/","1/2/3")
1
 
$ a = "HeLlO"
$  write sys$output f$edit(a,"UPCASE")
HELLO
$ write sys$output f$edit(a,"LOWERCASE")
hello
 
  • removing double spaces from a string:
SMAN43$ a = "           hello     there"
SMAN43$ write sys$output f$edit(a,"compress")
 hello there
 
  • removing all spaces from a string:
SMAN43$ a = "           hello     there"
SMAN43$ write sys$output f$edit(a,"collapse")
hellothere
 
$ a = "Hello! How can I help?"
$ write sys$output f$edit(a,"uncomment")
Hello
 

String Comparison

The following operators can be used to compare strings in an IF statement:

Operator Meaning
.EQS. Equal
.NES. Not equal
.GES. Greater or equal
.GTS. Greater
.LES. Less or equal
.LTS. Less

For example:

$ NAME = "Jane"
$ IF NAME .EQS. "John" THEN SHOW TIME
$

In the example above, SHOW TIME is not executed because string "Jane" is not equal to "John".

Strings vs Integers

You can convert an integer into a string with F$STRING(). To convert a string into an integer, use F$INTEGER(). If the string being converted to an integer consists of numbers, it will be converted to that number:

SMAN43$ a = "12"
SMAN43$ b = f$integer(a)
SMAN43$ show sym b
  B = 12   Hex = 0000000C  Octal = 00000000014
 

If it contains alphanumeric characters, the evaluation of the Boolean value is used: all strings beginning with "Y" or "y" are evaluated as 1 and all other strings as 0:

SMAN43$ a = "a12"
SMAN43$ b = f$integer(a)
SMAN43$ show sym b
  B = 0   Hex = 00000000  Octal = 00000000000
 

String Symbols

To define a string symbol, you can use either a colon (:) before the equals sign or the double quotes (") around the equivalence string. The difference is that the quotes preserve the case of the string and the colon does not:

SMAN43$ a := Hello
SMAN43$ show sym a
  A = "HELLO"
SMAN43$ a = "Hello"
SMAN43$ show sym a
  A = "Hello"
 

Literal Quotes

To put double quotes inside of a string, use two sets of double quotes:

$ a = "Here are some ""quotes"" for you"
$ show sym a
  A = "Here are some "quotes" for you"
 

A single double quote (") symbol can be defined like this:

$ a = """"
$ show sym a
  A = """
 

To put single quotes inside of a string, define a symbol for a single quote and then concatenate that with the other strings:

$ a = "'"
$ show sym a
  A = "'"
$ b = "Here are some "+a+"single quotes"+a+" for you"
$ show sym b
  B = "Here are some 'single quotes' for you"
 

The reason you cannot just put single quotes inside of a string is that they are used to force symbol substitution inside of a string.