FU_Strings Module

Useful tools to manipulate strings in Fortran programs.


Uses


Contents


Interfaces

public interface zfill

Adds zeros at the beginning of a string.

Syntax

 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.

Example

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
  • private pure function zfill_i8(str, l) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=MAX)

    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.

  • private pure function zfill_i16(str, l) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=MAX)

    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.

  • private pure function zfill_i32(str, l) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=MAX)

    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.

  • private pure function zfill_i64(str, l) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=MAX)

    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.

public interface splitstr

Splits a string and returns the portion selected by the user.

Syntax

 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.

Example

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
  • private pure function splitstr_i8(str, fieldNumber, delimiter, rev, mergedelim) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    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.

  • private pure function splitstr_i16(str, fieldNumber, delimiter, rev, mergedelim) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    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.

  • private pure function splitstr_i32(str, fieldNumber, delimiter, rev, mergedelim) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    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.

  • private pure function splitstr_i64(str, fieldNumber, delimiter, rev, mergedelim) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    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.

public interface num2str

Converts an integer or real variable into a string variable. Useful to open files named sequentially.

Syntax

 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.

Example

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
  • private pure function num2str_i8(num) result(str)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i8), intent(in) :: num

    Number to convert to string.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_i16(num) result(str)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i16), intent(in) :: num

    Number to convert to string.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_i32(num) result(str)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i32), intent(in) :: num

    Number to convert to string.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_i64(num) result(str)

    Arguments

    Type IntentOptional Attributes Name
    integer(kind=i64), intent(in) :: num

    Number to convert to string.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_sp(num, formato) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_dp(num, formato) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

  • private pure function num2str_qp(num, formato) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number

public interface int2str0

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.

Syntax

 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.

Example

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
  • private pure function int2str0_i8(integ, total_length) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number.

  • private pure function int2str0_i16(integ, total_length) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number.

  • private pure function int2str0_i32(integ, total_length) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number.

  • private pure function int2str0_i64(integ, total_length) result(str)

    Arguments

    Type IntentOptional 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.

    Return Value character(len=:), ALLOCATABLE

    String containing the number.

public interface str2num

Converts a string into an integer or real number as specified by the type of variable mold.

Syntax

 t = str2num(str, mold)

Where:

  • str: String to convert to number
  • 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.

It returns an integer or real with the number contained in the string.

Example

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
  • private elemental function str2num_i8(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value integer(kind=i8)

    The number of the input string.

  • private elemental function str2num_i16(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value integer(kind=i16)

    The number of the input string.

  • private elemental function str2num_i32(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value integer(kind=i32)

    The number of the input string.

  • private elemental function str2num_i64(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value integer(kind=i64)

    The number of the input string.

  • private elemental function str2num_sp(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value real(kind=sp)

    The number of the input string.

  • private elemental function str2num_dp(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value real(kind=dp)

    The number of the input string.

  • private elemental function str2num_qp(str, mold) result(res)

    Arguments

    Type IntentOptional 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.

    Return Value real(kind=qp)

    The number of the input string.


Functions

public pure function mergeChars(str, c) result(res)

Author
Emilio Castro.
Date
14/08/2020.
License
MIT.
Version
1.0.

Merge characters in a string if they are contiguous.

Read more…

Arguments

Type IntentOptional 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.

Return Value character(len=:), ALLOCATABLE

String with the selected character contiguous duplications removed.

public elemental function startsWith(str, substr) result(res)

Author
Emilio Castro.
Date
07/05/2020.
License
MIT.
Version
1.0.

Checks if a string starts with a given substring.

Read more…

Arguments

Type IntentOptional 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.

Return Value logical

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.

public elemental function endsWith(str, substr) result(res)

Author
Emilio Castro.
Date
07/05/2020.
License
MIT.
Version
1.0.

Checks if a string ends with a given substring.

Read more…

Arguments

Type IntentOptional 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.

Return Value logical

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.

public pure function replace(str, search, repla) result(res)

Author
Emilio Castro.
Date
10/07/2020.
License
MIT.
Version
1.0.

Searches and replaces a substring in a string

Read more…

Arguments

Type IntentOptional 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.

Return Value character(len=:), ALLOCATABLE

Modified string.

public pure function upper(str) result(res)

Author
Emilio Castro.
Date
20/08/2020.
License
MIT.
Version
1.0.

Converts a string to uppercase characters.

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

String to convert to uppercase characters.

Return Value character(len=:), ALLOCATABLE

String converted to uppercase characters.

public pure function lower(str) result(res)

Author
Emilio Castro.
Date
20/08/2020.
License
MIT.
Version
1.0.

Converts a string to lowercase characters.

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str

String to convert to lowercase characters.

Return Value character(len=:), ALLOCATABLE

String converted to lowercase characters.

public pure function cistrcmp(str1, str2) result(res)

Author
Emilio Castro.
Date
20/08/2020.
License
MIT.
Version
1.0.

Case-independent string comparison.

Read more…

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str1

First string to compare.

character(len=*), intent(in) :: str2

Second string to compare

Return Value logical

True if both strings are equal independently of the case. False otherwise.