Useful tools to manipulate strings in Fortran programs.
Adds zeros at the beginning of a string.
newStr = zfill(str, l)
Where:
str
: String that the user wants to fill with leading zeros..l
: Integer indicating the length of the resuling string.If l
is greater than the length of str
, it returns a string of length l
with str
padded with zeros at the beginning. If l
is lower or equal to the
length of str
, it returns str
.
The following program fills a string with leading zeros:
PROGRAM zfillExample
USE FU_Strings, ONLY: zfill
IMPLICIT NONE
WRITE(*,*) zfill('myStr', 12)
END PROGRAM zfillExample
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to fill with leading zeros. |
||
integer(kind=i8), | intent(in) | :: | l |
Integer indicating the length of the resuling string. |
If l
is greater than the length of str
, it returns a string of length l
with str
padded with zeros at the beginning. If l
is lower or equal to the
length of str
, it returns str
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to fill with leading zeros. |
||
integer(kind=i16), | intent(in) | :: | l |
Integer indicating the length of the resuling string. |
If l
is greater than the length of str
, it returns a string of length l
with str
padded with zeros at the beginning. If l
is lower or equal to the
length of str
, it returns str
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to fill with leading zeros. |
||
integer(kind=i32), | intent(in) | :: | l |
Integer indicating the length of the resuling string. |
If l
is greater than the length of str
, it returns a string of length l
with str
padded with zeros at the beginning. If l
is lower or equal to the
length of str
, it returns str
.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to fill with leading zeros. |
||
integer(kind=i64), | intent(in) | :: | l |
Integer indicating the length of the resuling string. |
If l
is greater than the length of str
, it returns a string of length l
with str
padded with zeros at the beginning. If l
is lower or equal to the
length of str
, it returns str
.
Splits a string and returns the portion selected by the user.
t = splitstr(str, fieldNumber, delimiter, rev, mergedelim)
Where:
str
: String that the user whants to split.fieldNumber
: Integer indicating which of the divisions to return.delimiter
: String that the user wants to use as a delimiter for splitting.
Optional parameter. Default is Space.rev
: If true start spliting by the end of the string.
Optional parameter. Default is False.mergedelim
: If true, contiguous delimiters in the string are merged before splitting.
Optional parameter. Default is False.It returns a string with the selected part of str. If the fieldNumber does not exists or if the delimiter does not exists it returns an empty string.
The following program extracts some portions of a text:
PROGRAM splitstrExample
USE FU_Strings, ONLY: splitstr
IMPLICIT NONE
CHARACTER(LEN=:), ALLOCATABLE :: text
CHARACTER(LEN=:), ALLOCATABLE :: portion
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, &
&sed do eiusmod tempor incididunt ut labore et dolore magna al&
&iqua. Ut enim ad minim veniam, quis nostrud exercitation ulla&
&mco laboris nisi ut aliquip ex ea commodo consequat. Duis aut&
&e irure dolor in reprehenderit in voluptate velit esse cillum&
& dolore eu fugiat nulla pariatur. Excepteur sint occaecat cup&
&idatat non proident, sunt in culpa qui officia deserunt molli&
&t anim id est laborum."
portion = splitstr(text, 1)
WRITE(*,*) portion
portion = splitstr(text, 2, rev = .True.)
WRITE(*,*) portion
portion = splitstr(text, 6, delimiter = "l"
WRITE(*,*) portion
portion = splitstr(text, 7, delimiter = "l"
WRITE(*,*) portion
portion = splitstr(text, 7, delimiter = "l", mergedelim = .True.)
WRITE(*,*) portion
END PROGRAM splitstrExample
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to split. |
||
integer(kind=i8), | intent(in) | :: | fieldNumber |
Integer indicating which of the divisions to return. |
||
character(len=*), | intent(in), | optional | :: | delimiter |
String that the users wants to use as a delimiter for splitting. Optional parameter. Default is Space. |
|
logical, | intent(in), | optional | :: | rev |
If true start spliting by the end of the string. Optional parameter. Default is False. |
|
logical, | intent(in), | optional | :: | mergedelim |
If true, contiguous delimiters in the string are merged before splitting. Optional parameter. Default is False. |
A string with the selected part of str. If the fieldNumber does not exists or if the delimiter does not exists it returns an empty string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to split. |
||
integer(kind=i16), | intent(in) | :: | fieldNumber |
Integer indicating which of the divisions to return. |
||
character(len=*), | intent(in), | optional | :: | delimiter |
String that the users wants to use as a delimiter for splitting. Optional parameter. Default is Space. |
|
logical, | intent(in), | optional | :: | rev |
If true start spliting by the end of the string. Optional parameter. Default is False. |
|
logical, | intent(in), | optional | :: | mergedelim |
If true, contiguous delimiters in the string are merged before splitting. Optional parameter. Default is False. |
A string with the selected part of str. If the fieldNumber does not exists or if the delimiter does not exists it returns an empty string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to split. |
||
integer(kind=i32), | intent(in) | :: | fieldNumber |
Integer indicating which of the divisions to return. |
||
character(len=*), | intent(in), | optional | :: | delimiter |
String that the users wants to use as a delimiter for splitting. Optional parameter. Default is Space. |
|
logical, | intent(in), | optional | :: | rev |
If true start spliting by the end of the string. Optional parameter. Default is False. |
|
logical, | intent(in), | optional | :: | mergedelim |
If true, contiguous delimiters in the string are merged before splitting. Optional parameter. Default is False. |
A string with the selected part of str. If the fieldNumber does not exists or if the delimiter does not exists it returns an empty string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to split. |
||
integer(kind=i64), | intent(in) | :: | fieldNumber |
Integer indicating which of the divisions to return. |
||
character(len=*), | intent(in), | optional | :: | delimiter |
String that the users wants to use as a delimiter for splitting. Optional parameter. Default is Space. |
|
logical, | intent(in), | optional | :: | rev |
If true start spliting by the end of the string. Optional parameter. Default is False. |
|
logical, | intent(in), | optional | :: | mergedelim |
If true, contiguous delimiters in the string are merged before splitting. Optional parameter. Default is False. |
A string with the selected part of str. If the fieldNumber does not exists or if the delimiter does not exists it returns an empty string.
Converts an integer or real variable into a string variable. Useful to open files named sequentially.
t = num2str(num, format)
Where:
num
: Real or Integer to convert to a string.format
: Format to use in the string variable. Only for real numbers.It returns a string containing the number.
The following program a converts an integer and a real to a string, using them to create a filename:
PROGRAM num2strExample
USE FU_Strings, ONLY: num2str
IMPLICIT NONE
REAL :: temperature
INTEGER :: case_number
CHARACTER(LEN=:), ALLOCATABLE :: filename
temperature = 293.75
case_number = 17
filename = 'Case_'//num2str(case_number)// &
'_Temp_'//num2str(temperature, "F4.0")//'txt'
WRITE(*,*) filename
END PROGRAM num2strExample
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i8), | intent(in) | :: | num |
Number to convert to string. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i16), | intent(in) | :: | num |
Number to convert to string. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i32), | intent(in) | :: | num |
Number to convert to string. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i64), | intent(in) | :: | num |
Number to convert to string. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=sp), | intent(in) | :: | num |
Number to convert to string. |
||
character(len=*), | intent(in) | :: | formato |
Format to use in the string variable. Only for real numbers. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=dp), | intent(in) | :: | num |
Number to convert to string. |
||
character(len=*), | intent(in) | :: | formato |
Format to use in the string variable. Only for real numbers. |
String containing the number
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
real(kind=qp), | intent(in) | :: | num |
Number to convert to string. |
||
character(len=*), | intent(in) | :: | formato |
Format to use in the string variable. Only for real numbers. |
String containing the number
Converts an integer variable into a string variable, filling with leading zeros up to the limit imposed by the user. Useful to open files named sequentially with leading zeros in the name.
t = int2str0(integ, total_length)
Where:
integ
: Integer number to convert. This number MUST be positive.format
: Number of digits to use, including leading zeros. This number MUST be positive.It returns a string containing the number.
The following program a converts an integer to a number with leading zeros to create sequential filenames
PROGRAM int2str0Example
USE FU_Strings, ONLY: int2str0
IMPLICIT NONE
INTEGER :: i
INTEGER :: total_length
CHARACTER(LEN=:), ALLOCATABLE :: filename
total_length = 5
DO i = 1, 25
filename = int2str0(i, total_length)//'.dat'
WRITE(*,*) filename
END DO
END PROGRAM int2str0Example
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i8), | intent(in) | :: | integ |
Integer number to convert. This number MUST be positive. |
||
integer(kind=i8), | intent(in) | :: | total_length |
Number of digits to use, including leading zeros. This number MUST be positive. |
String containing the number.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i16), | intent(in) | :: | integ |
Integer number to convert. This number MUST be positive. |
||
integer(kind=i16), | intent(in) | :: | total_length |
Number of digits to use, including leading zeros. This number MUST be positive. |
String containing the number.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i32), | intent(in) | :: | integ |
Integer number to convert. This number MUST be positive. |
||
integer(kind=i32), | intent(in) | :: | total_length |
Number of digits to use, including leading zeros. This number MUST be positive. |
String containing the number.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=i64), | intent(in) | :: | integ |
Integer number to convert. This number MUST be positive. |
||
integer(kind=i64), | intent(in) | :: | total_length |
Number of digits to use, including leading zeros. This number MUST be positive. |
String containing the number.
Converts a string into an integer or real number as specified by the type of variable mold.
t = str2num(str, mold)
Where:
str
: String to convert to numbermold
: Real or integer value to identify the type and kind of the output.
It is only used to set the type of the return value, so it can be any value.It returns an integer or real with the number contained in the string.
The following program converts a string to a real number
PROGRAM str2numExample
USE FU_Strings, ONLY: str2num
IMPLICIT NONE
REAL :: f
CHARACTER(LEN=:), ALLOCATABLE :: s
s = '293.75'
f = str2num(s, f)
WRITE(*,*) f
END PROGRAM str2numExample
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
integer(kind=i8), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
integer(kind=i16), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
integer(kind=i32), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
integer(kind=i64), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
real(kind=sp), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
real(kind=dp), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to number. |
||
real(kind=qp), | intent(in) | :: | mold |
Real or integer value to identify the type and kind of the output. It is only used to set the type of the return value, so it can be any value. |
The number of the input string.
Merge characters in a string if they are contiguous.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to search inside for contiguous duplicated characters. |
||
character(len=*), | intent(in) | :: | c |
Character to search for contiguous duplications. |
String with the selected character contiguous duplications removed.
Checks if a string starts with a given substring.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to check how it starts. It can be an array. |
||
character(len=*), | intent(in) | :: | substr |
Substring to search to check if str starts with it. |
True if the string starts with the substring and False otherwise. If substr is empty it returns True. If the input is an array, the returned values will also be in an array.
Checks if a string ends with a given substring.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String that the user wants to check how it ends. It can be an array. |
||
character(len=*), | intent(in) | :: | substr |
Substring to search to check if str ends with it. |
True if the string ends with the substring and False otherwise. If substr is empty it returns True. If the input is an array, the returned values will also be in an array.
Searches and replaces a substring in a string
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to modify |
||
character(len=*), | intent(in) | :: | search |
String to search for in str. |
||
character(len=*), | intent(in) | :: | repla |
String to replace in str. |
Modified string.
Converts a string to uppercase characters.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to uppercase characters. |
String converted to uppercase characters.
Converts a string to lowercase characters.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str |
String to convert to lowercase characters. |
String converted to lowercase characters.
Case-independent string comparison.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | str1 |
First string to compare. |
||
character(len=*), | intent(in) | :: | str2 |
Second string to compare |
True if both strings are equal independently of the case. False otherwise.