Loading...
Searching...
No Matches
String

Definition

Minimal yet powerful variable-length string type with modern Fortran features. This module implements a lightweight string derived type that behaves like a true variable-length character string while remaining fully compatible with intrinsic Fortran character operations.

Features:

The design is intentionally minimal — it provides only what's necessary for robust string handling in scientific and preprocessing applications, avoiding the bloat of larger string libraries while remaining fast and standards-compliant.

Note
All procedures are pure or elemental when possible for maximum performance and usability in array contexts.

Examples

Basic Usage
type(string) :: s, t
character(:), allocatable :: line
s = 'Hello' ! Assignment from literal
t = s // ' World!' ! Concatenation
print *, t%chars ! Output: Hello World!
if (s == 'Hello') then
print *, 'Equal'
else
print *, 'Case sensitive'
endif
print *, len(t) ! -> 12
print *, len_trim(t) ! -> 12
...
Array and Container Support
type(string) :: words(3)
logical :: found
words = [string('apple'), string('banana'), string('cherry')]
found = words .contains. 'banana' ! --> .true.
found = words .contains. string('date') ! --> .false.
...
Advanced: Source Code Processing
character(len=:), allocatable :: code_line
code_line = uppercase('program hello_world ! comment') ! --> 'PROGRAM HELLO_WORLD ! comment'
...

Data Types

type  string
 Represents text as a sequence of ASCII code units. The derived type wraps an allocatable character array. More...
 
interface  len
 Return the length of a string. More...
 
interface  len_trim
 Return the trimmed length of a string. More...
 
interface  trim
 Return the trimmed string. More...
 
interface  operator(//)
 Concatenation operator. More...
 
interface  operator(.contains.)
 Check whether a string belongs to a list or not. More...
 

Methods

◆ concat()

character(:) function, allocatable, public concat ( character(*), intent(in) str1,
character(*), intent(in) str2 )

Smart concatenation that removes continuation markers (&) and handles line-continuation rules.

Parameters
[in]str1first line
[in]str2second line
Returns
Concatenated string with proper continuation handling

Remarks

Definition at line 535 of file string.f90.

◆ head()

character function, public head ( character(*), intent(in) str)

Returns the first non-blank character of a string.

Parameters
[in]strinput string
Returns
First character (space if empty)

Remarks

Definition at line 502 of file string.f90.

◆ previous()

character(1) function, public previous ( character(*), intent(in) line,
integer, intent(inout) pos )

Returns the previous non-blank character before position pos (updates pos).

Parameters
[in]lineinput line
[in,out]poscurrent position (moved backward)
Returns
Previous non-blank character

Remarks

Definition at line 641 of file string.f90.

◆ starts_with()

logical function, public starts_with ( character(*), intent(in) str,
character(*), intent(in) arg1,
integer, intent(out), optional idx )

Checks if a string starts with a given prefix Returns .true. if the string str (after trimming leading/trailing whitespace) begins exactly with the substring arg1. The function uses index() after trimming both strings with trim(adjustl()).

Parameters
[in]strThe string to be tested
[in]arg1The prefix to look for at the beginning of str
[out]idx(optional) If present, receives the starting position of arg1 in the trimmed string (will be 1 if the function returns .true., otherwise >1 or 0)
Returns
.true. if str starts with arg1 (after trimming), .false. otherwise

Note

  • Leading and trailing whitespace of both str and arg1 is ignored
  • Comparison is case-sensitive
  • Empty arg1 will always return .true. (any string starts with empty string)

Warning The returned index (when requested) is the position after trimming of the input string, not in the original untrimmed string.

Examples

character(80) :: line = ' hello world '
logical :: ok
integer :: pos
ok = starts_with(line, 'hello') ! → .true.
ok = starts_with(line, 'hello', pos) ! → .true. and pos = 1
ok = starts_with(line, 'world') ! → .false.
ok = starts_with(' test123 ', 'test') ! → .true.
...

Remarks

Definition at line 484 of file string.f90.

◆ tail()

character function, public tail ( character(*), intent(in) str)

Returns the last non-blank character of a string.

Parameters
[in]strinput string
Returns
Last character (space if empty)

Remarks

Definition at line 517 of file string.f90.

◆ uppercase()

pure character(len_trim(str)) function, public uppercase ( character(*), intent(in) str)

Convert string to upper case (respects contents of quotes).

Parameters
[in]strinput string
Returns
Upper-case version of the string

Examples

character(*), parameter :: input = 'test'
character(:), allocatable :: output
output = uppercase(input)
if (output == 'TEST') print*, 'OK'

Remarks

Definition at line 582 of file string.f90.

◆ writechk()

subroutine, public writechk ( integer, intent(in) unit,
character(*), intent(in) str )

Write a long line split into chunks of size CHKSIZE with continuation (&).

Parameters
[in]unitlogical unit
[in]strstring to write

Remarks

Definition at line 618 of file string.f90.