startsWith Function

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

Checks if a string starts with a given substring. It can be an array of string.

Syntax

 l = startsWith(str, substr)

Where:

  • str: String that the user wants to check how it starts. It can be an array.
  • substr: Substring to search for to check if str starts with it.

It returns 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.

Example

The following program checks if a string starts with a substring:

 PROGRAM startsWithExample
    USE FU_Strings, ONLY: startsWith
    IMPLICIT NONE
    CHARACTER(LEN=:), ALLOCATABLE :: text1, text2
    text1 = 'String1'
    text2 = 'St'
    IF (startsWith(text1, text2)) THEN
       WRITE(*,*) 'String starts with St'
    ELSE
       WRITE(*,*) 'String does not start with St'
    END IF
 END PROGRAM startsWithExample

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.


Contents