Re: appending to a string

From: Rich Townsend (rhdt_at_barVOIDtol.udel.edu)
Date: 03/02/04


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


Relevant Pages

  • Re: how do i import a text file with line wraps into a table
    ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: Email Merge in Word
    ... WHERE mystring is not null AND trim'' ... If the address looks blank but len) is> 0 then the field probably contains invisible non-space characters. ... Different software packages can treat "null", "a string set to ''", and "a string containing white space differently, and may also treat variable-length and fixed-length data differently in this respect. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Creating a derived object from a base object
    ... namespace MyString ... public class MyString ... private String _s; ... can't cast the result to a MyString since I can't cast a base type to ...
    (microsoft.public.dotnet.languages.csharp)