Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Multiline strings

dzuba
Beginner
1,070 Views
Hello!

I have to create multiline string (for edit box).
CHARACTER(1000) M_STR

This is what I whant to have:
M_STR = "111 222 333"C.

Now:
S1 = "111"
S2 = "222"
S3 = "333"

Two operations below works wrong. I have "111 " as result.
M_STR = S1 // " "C // S2 // " "C // S3
WRITE(M_STR, *) S1, " "C, S2, " "C, S3

So, how can I concatinate several strings with between them??

Thanks!
0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,070 Views
I think you want:

M_STR = TRIM(S1) // " "C // TRIM(S2) // " "C // TRIM(S3)
0 Kudos
dzuba
Beginner
1,070 Views


Steve_Lionel wrote:
I think you want:

M_STR = TRIM(S1) // " "C // TRIM(S2) // " "C // TRIM(S3)




Steve! Thanks, but I think problem is in "//" operation, not in spaces btrween strings :).

Thees two operations are not equals
1 M_STR = " 111 " // " "C // " 222 " // " "C
2 M_STR = " 111 222 "C

in first case the result is " 111 ".
WHY ?
0 Kudos
Steven_L_Intel1
Employee
1,070 Views
You have not shown a buildable example. When I create one from your excerpts, adding the TRIM, it works for me. Here's my example:

CHARACTER(1000) M_STR
character*10 s1, s2, s3

!M_STR = "111 222 333"C.

!Now:
S1 = "111"
S2 = "222"
S3 = "333"

!Two operations below works wrong. I have "111 " as result.
M_STR = TRIM(S1) // " "C // TRIM(S2) // " "C // TRIM(S3)
write (*,*) trim(m_str)
WRITE(M_STR, *) TRIM(S1), " "C, TRIM(S2), " "C, TRIM(S3)
write (*,*) trim(m_str)
end


When I run this I get:

 111
222
333
111
222
333

(The extra blank in the second example is due to your use of list-directed formatting in the WRITE to M_STR).
0 Kudos
Les_Neilson
Valued Contributor II
1,070 Views

dzuba,

How have you declared S1, S2, and S3 ?

Les

0 Kudos
anthonyrichards
New Contributor III
1,070 Views

First of all, you are making your strings c-strings by adding 'C' at the end.

This will cause the concatenated string to end at the first null character it finds, hence you are not getting the concatenated answer you want.

Secondly, if you want to use the ' ' way of generating carriage return and line feed, the whole character string in which they are used MUST be visible to the compiler at compile time, as that is when the ' c' is converted to the CHAR(13) and CHAR(10) characters that are carriage return and line feed. I discovered all this recently when trying to generate a multiline messge for use in MessageBox.

If you want new lines just use

character*2 crlf = CHAR(13)//CHAR(10)

STRING = STRING1//CRLF//STRING2//CRLF//STRING3//char(0)

And so on, where the component STRINGS are NOT terminated with CHAR(0) (the null terminator required at the end of the concatenated string to make it a C-string acceptable to windows functions like MessageBox). Also, TRIM the strings as Steve suggests before using them

You can also use internal WRITES as

WRITE(MESSAGE, '(a10,A2,A10,A2,A10)' ) string1,crlf,string2,crlf,string3//char(0)

0 Kudos
Steven_L_Intel1
Employee
1,070 Views
Doh! Absolutely right. Why didn't I notice that?

I'll comment that I like to make CLRF a PARAMETER.
0 Kudos
dzuba
Beginner
1,070 Views
The problem solved,

anthonyrichards,
I did't noticed that " "C is equals to
char(10)//char(13)//char(0)
So, the string was wrapped after first char(0) when I passed it to rhe edit box.

Tank you.
0 Kudos
Reply