trim(str);len(str) /= len(trim(str))

trim(str);len(str) /= len(trim(str))

Imagen de wkramer

Hello,

Why is there a difference in returned length between:

character(28) :: str='I-have-blanks-behind-me    '
string=trim(string)
l=len(string) ! => l=28


and

l=len(trim(string)) ! => l=23


and of course

l=len_trim(string)  ! => l=23


same situation for

string=trim(string)
l=index(string," ")  ! => l=24


whereas

l=index(trim(string)," ")  ! => l=0



This seems to be a bit inconsistent.
I am just curious why it is implemented this way.
It appears that during the assignment:

string=trim(string)

the string is padded with blanks. Is this Fortran standard?

Thanks,

Walter Kramer

publicaciones de 3 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.
Imagen de wkramer

OK, I am sorry. I should have thought before acting.

I understand that assigning a trimmed string to a string with a (longer) fixed length requires some form of padding. Padding with blanks seems the logical choice.

Walter Kramer

Imagen de james@elementyl.com

Yes, this is all standard. :-) The purpose of LEN is to return the total length of the string, including any trailing spaces. LEN_TRIM gives you the length without trailing spaces. As Fortran pads strings with spaces, it is nice to have both, one to tell you how much space you have available in the string variable, and another to tell you how much of that string variable is actually worth formatting.

The TRIM function returns a string of LEN_TRIM length of the original string, without the trailing spaces of course. But if you assign this back to the original string, the result will be the same as the TRIMmed string is space padded. So in your example, string=TRIM(string) does not change string, thus LEN(string) will not change either.

James

Inicie sesión para dejar un comentario.