Re: appending to a string
From: Rich Townsend (rhdt_at_barVOIDtol.udel.edu)
Date: 03/02/04
- Next message: Jugoslav Dujic: "Re: appending to a string"
- Previous message: Paul van Delst: "Re: appending to a string"
- In reply to: Bart Vandewoestyne: "appending to a string"
- Next in thread: Catherine Rees Lay: "Re: appending to a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 02 Mar 2004 10:44:40 -0500
Bart Vandewoestyne wrote:
> I'm trying to append some string data to an already existing string, but
> the following code doesn't work:
>
> program test_string
>
> implicit none
>
> character(len=255) :: mystring
>
> mystring = "Hello"
>
> print *, mystring
>
> mystring = mystring // "World"
>
> print *, mystring
>
> end program test_string
>
>
> I guess I'm missing something here, but after some Googling i still
> couldn't figure out what...
>
> How to do this?
>
> Thanks,
> Bart
>
This comes down to the way in which Fortran handles strings. Unlike C
and related languages, the length of a Fortran string is fixed
throughout the program unit (ie, main program, subroutine, function etc)
in which it appears.
Therefore, when you write
mystring = "Hello"
the variable mystring *still* has the sam length (255) which it was
declared with. The first five characters are the word "Hello", and the
remaining characters are blanks.
This explains why the statement
mystring = mystring // "World"
doesn't work. The concatenation on the right hand side produces a
temporary string with the first five characters being the word "Hello",
followed by 250 (=255-5) blanks, followed by the word "World" -- a total
of 260 characters. When you store this back into mystring, however,
there is only room for the first 255 characters (remember, the length of
mystring is fixed at 255!); therefore, the last 5 characters of the
right hand side are truncated, and the trailing "World" is lost.
To fix this problem is easy; use the TRIM() intrinsic function, which
returns the supplied string with the trailing blanks removed. You can
check on the removal of these blanks using the LEN() intrinsic:
mystring = "Hello"
print *,LEN(mystring)
print *,LEN(TRIM(mystring))
The first print statement will return 255, the declared length of
mystring. The second statement will return 5, the length of the string
temporary (="Hello") returned by TRIM().
So your program should be modified to read something like this:
-- program test_string implicit none character(len=255) :: mystring mystring = "Hello" print *, TRIM(mystring) mystring = TRIM(mystring) // " World" print *, TRIM(mystring) end program test_string -- Note that I've assumed you want a space between "Hello" and "World". Note also that I've used TRIM() in the print statements also, to tidy up the output (otherwise, you are printing out 255-character strings all of the time). Hope this helps, cheers, Rich
- Next message: Jugoslav Dujic: "Re: appending to a string"
- Previous message: Paul van Delst: "Re: appending to a string"
- In reply to: Bart Vandewoestyne: "appending to a string"
- Next in thread: Catherine Rees Lay: "Re: appending to a string"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|