printing - How to combine two strings in Fortran -


here's example in python3 of want in fortran:

str1 = "hello" str2 = " world!" print(str1 + str2)  # , result "hello world!" 

when do:

print "(a)", str1, str2 

it puts on separate line. if knows how please answer.

the literal answer string concatenation, using // operator, given in answer. note, particularly, want trim first argument.

but there interesting concept question raises, , format reversion.

with format (a) have 1 format item. in output list str1, str2 have 2 output items. in general output statement apply each format item (with repeat counts) corresponding output item. so, str1 processed first format item a, , string appears.

come second output item str2 we've used single format item, reaching end of format item list. result see format reversion: is, go first item in list. after, crucially, start new line.

so, if want print 2 items 1 line (with no space or blank line between them) use (neglecting trimming clarity)

print "(a)", str1//str2 

or use format hasn't reversion

print "(2a)", str1, str2 print "(a, a)", str1, str2 

the first concatenates 2 character variables give one, longer, printed single output item. second prints both individually.

coming particular example

character(12), parameter :: str1="hello"    ! intentionally longer - trailing blanks character(12), parameter :: str2=" world!"  print "(2a)", trim(str1), trim(str2) end 

will have output like

hello world! 

with middle space because trim won't remove leading space str2. more widely, though won't have leading space there us, , want add in output.

naturally, concatenation still works (i'm assuming no-trimming)

character(*), parameter :: str1="hello"    ! no trailing blank character(*), parameter :: str2="world!"  print "(a)", str1//" "//str2 end 

but can choose our format, using x edit descriptor, add space

print "(2(a,1x))", str1, str2 print "(a,1x,a)", str1, str2 print "(2(a,:,1x))", str1, str2 

where final 1 has useful colon edit descriptor (outside scope of answer).


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -