Skip to content

Commit e902e5c

Browse files
committed
join: add example
1 parent 0d27f92 commit e902e5c

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

doc/specs/stdlib_strings.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,7 @@ The result is of the same type as the elements of `strings` (`type(string_type)`
494494
#### Example
495495

496496
```fortran
497-
! Example usage:
498-
program test_join
499-
type(string_type) :: result
500-
type(string_type), dimension(3) :: words = [string_type('hello'), string_type('world'), string_type('fortran')]
501-
result = join_string(words, ', ') ! Joins with comma and space
502-
print *, result ! Output: "hello, world, fortran"
503-
end program test_join
497+
{!example/strings/example_join.f90!}
504498
```
505499

506500
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
@@ -570,3 +564,9 @@ Pure function.
570564
#### Result value
571565

572566
The result is a `character(kind=c_char)` array with a dimension of `len(value) + 1` to accommodate the null terminator.
567+
568+
#### Example
569+
570+
```fortran
571+
{!example/strings/example_to_c_string.f90!}
572+
```

example/strings/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ADD_EXAMPLE(chomp)
22
ADD_EXAMPLE(count)
33
ADD_EXAMPLE(ends_with)
44
ADD_EXAMPLE(find)
5+
ADD_EXAMPLE(join)
56
ADD_EXAMPLE(padl)
67
ADD_EXAMPLE(padr)
78
ADD_EXAMPLE(replace_all)

example/strings/example_join.f90

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
program example_join
2+
use stdlib_strings, only: join
3+
implicit none
4+
5+
character(len=:), allocatable :: line
6+
character(*), parameter :: words(3) = [character(7) :: "Hello", "World", "Fortran"]
7+
8+
! Default separator (space)
9+
line = join(words)
10+
print *, "'" // line // "'" !! 'Hello World Fortran'
11+
12+
! Custom separator
13+
line = join(words, "_")
14+
print *, "'" // line // "'" !! 'Hello_World_Fortran'
15+
16+
! Custom 2-character separator
17+
line = join(words, ", ")
18+
print *, "'" // line // "'" !! 'Hello, World, Fortran'
19+
20+
stop 0
21+
end program example_join

0 commit comments

Comments
 (0)