Character strings

Introduction

The statements:

CHARACTER c
c="A"

declare c to be a single character and assigns the letter A to it.

The statements:

CHARACTER(LEN=20)::str
str="A test string"

declare str to be a character string consisting of 20 characters and assigns the given string to it.

A double colon (::) can be used to separate any of the intrinsic types INTEGER, REAL, LOGICAL, COMPLEX and CHARACTER from the list that flows. It is mandatory when the type is given additional attributes.

 

Assignment

Suppose that str1 is assigned to str2:

str2=str1

If str1 is shorter than str2 then str2 is padded out with trailing spaces. If str1 is longer than str2 then str1 is truncated to fit.

 

Concatenation

A double oblique (//) is used with character strings to denote the operation of concatenation thus:

str=str1//str2

The use of a variable on both sides of an assignment statement is not permitted. For example:

str=str//"."

causes an error condition.

 

Sub strings

A sub string of the variable str is obtained using the notation str(pos1:pos2) where pos1 and pos2 represent INTEGER expressions. pos1 and pos2 denote the positions in the string counting from 1 (unity) at the first character. For example:

str="A test string"
str1=str(3:6) !str1="test"
str2=str(:6) !str2="A test"
str3=str(3:) !str3="test string"

The resulting strings will be truncated or padded with trailing spaces depending on their lengths.

If pos1 is greater than pos2 then a string of zero length is obtained.

 

Intrinsic functions

LEN(str)  returns the number of characters in str.

LEN_TRIM(str)  returns the number of characters in str excluding trailing spaces

TRIM(str)  removes trailing spaces from str.

INDEX(str1,str2) returns the position of the first occurrence of str2 in str1.

SCAN(str1,str2) returns the position of the first occurrence in str1 of any of the characters of str2.

VERIFY(str1,str2) returns the position of the first occurrence in str1 of any of character not in str2.

INDEX, SCAN and VERIFY have optional third arguments that provide for scanning from the end.

More…

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited