Fortran: remove characters from a string

How to remove characters from a string?

For example, I have a line called "year" that I want to change from 4 characters to 2 characters. It is defined as follows:

character (4):: year = "2011"

How to truncate a string by 2 symbols, so that instead of year = "2011"it year = "11"?

+3
source share
3 answers

You really can use year(3:4); however your string will still contain four characters, i.e. it will contain two numbers and two spaces. To illustrate this, here is an example:

program trunc
   character(len=4) :: year = "2011"

   write(*,'(A,A,A)') '..', year, '..'
   year = year(3:4)
   write(*,'(A,A,A)') '..', year, '..'
end program trunc

Will print

..2011..
..11  ..

To really get "11"instead "11 ", you need to assign a value to a variable that can only contain two characters.

+2
source

I think it is year(3:4), but do not quote me on it;)

0
source

Use the following command:

trim(year(3:4))
0
source

Source: https://habr.com/ru/post/1784721/


All Articles