I. The difference between a Fortran string and a C string
Fortran's string processing ability is actually very weak, the syntax of the string is still very backward. It is the most significant difference from the C string: The FORTRAN string is fixed-length, without the/end character. In addition, Fortran does not differentiate between characters and strings. That is, there is no difference between ' abc ' and ' abc '.
Two. The definition of a Fortran string.
The FORTRAN string is fixed-length. Therefore, when declaring, you must specify a length. (if not specified, most compilers consider a string of length 1)
When declaring, you can do so in these formats:
1Integer, parameter:: L = -2Character:: s1* -="fcode.cn"3character* -:: S2 ="fcode.cn"4Character -):: s3 ="fcode.cn"5Character (len = -):: S4 ="fcode.cn"6Character (len = L):: S5 ="fcode.cn"
Currently, the author does not recommend the use of lines 2nd and 3rd. The way to the 4th line is not too bad. It is advisable to use line 5th, and line 6th shows that the length can use the parameter constant, which is appropriate for strings that require a large number of definitions of the same length.
Three. Length issues with FORTRAN strings
It is important to note thatOnce the length of the string is determined, it cannot be changed. I also feel that the grammar should be improved, very inconvenient.
For example, the example above.Character (Len =):: S4 = "fcode.cn" Although the following value "fcode.cn" has only 8 characters, but the variable is defined as 20 length, the actual S4 content is "fcode.cn" (later has 12 spaces)
If we want to output or use a string, we often need to remove the trailing spaces, at which point the trim function can be used.
Write (*,*) trim (S4)
In this way, only 8 characters with the content will be output, and the next 12 spaces will not be output.
In particular, it is important to note thatS4 = Trim (S4) Such sentences are meaningless.。 Because although the right side of the equation, the result of trim (S4) is 8 characters, but the string to the left of the equation, it is still 20 length. It is equivalent to S4 = "fcode.cn", so the content of S4 is still "fcode.cn" (readers can go back to see the green text above)
It makes us very puzzled. SoThe trim statement must be present every time the string is used.
Four. Fortran string connection (Append)
We often attach strings, such as "fcode.cn" in the back of a "/bbs", many beginners will use this code:
Character (Len =):: S4 = "fcode.cn"
S4 = S4//"/bbs"
After the output, it is found that the content of S4 remains unchanged. Did not become the desired "Fcode.cn/bbs". The reason is, in fact, a third problem. Let's look at the equation above: S4 = S4//"/bbs",The left S4 of the equals sign has 20 lengths,The 20 length of the S4 to the right of the equal sign plus the 4 length of "/bbs", altogether 24 lengths. The left 20 length is not fit to the right of the 24 length, so, S4 is still S4, the back of the "/bbs" is discarded due to storage.
The right way to be anchored is: S4 = Trim (S4)//"/bbs"
Conversely,s4 = "http//"//S4 does not need to write trim, please reader friends to think why?
Five. Left and right alignment of strings (ADJUSTL,ADJUSTR)
We often encounter this problem, in addition to the empty space in the back, sometimes there are spaces in front of the string. For example S4 = "Fortrancoder", a single trim result would be "Fortrancoder".
At this point we need the ADJUSTL function, we can write: Trim (Adjustl (S4)), or Adjustl (Trim (S4)) The order of the two does not matter, the result is the same.
The ADJUSTR function uses very few cases, usually not. It can turn "ABC" into "ABC".
Six. String and integral type, real type of mutual conversion
Fortran string and integer implementation of the conversion, is a very interesting thing. It does not provide a function to do, as in other languages. It is implemented using read and write read and write.
Look at the following example:
1 Program WWW_FCODE_CN2 Implicit None3 Real:: R4 Integer:: I5Character (Len = -):: C6c ="3.1415926"7Read (c, *) r!//converts a string C to a real number R8Write (*, *) R +1.09i =int(R-2.0) !//give I a valueTenWrite (c,'(I0)'I!//convert int i to string C Onec = Adjustl (Trim (c))//"St" AWrite (*, *) Trim (c) -End program WWW_FCODE_CN
The 7th line here looks like a read statement, which is actually a conversion. Read the value of R from string C. The reader can think of the string C at this time as a virtual file.
The 10th line here looks like a write statement, but it's still a conversion. This means: The value of I, written in the string C, the reader can still imagine that C is a virtual file.
Simply put, it isRead and write statements that can manipulate strings directly.A string can be considered a virtual file, a value obtained from a string, or read; Writes a value to a string, which is write.
Seven. An example
here, we enumerate an example of the actual string used.
Suppose you now have a file with the following contents:
This is a sample file
There may some comment text
time:13:23:42 RecordID: Weather:sunny
fcode.cn date:2014-02-38
The first two lines are comments, and it is possible that there are not two lines, and you are not sure how many rows.
Then there are some data that are grouped together. We need to find RecordID: The number behind, that is, 18. We are not sure in advance where RecordID is in the first few lines.
After finding it, we need to add 2014 to it, that is, 2014+18=2032.
Finally we open the File2031.dat file and start reading the data.
The above series of operations, if not the use of strings, I am afraid it will be difficult. Let's take a look at how to write code to do it. Use the various content mentioned in this article.
The code has a part of the comments, I believe readers can understand
1 Program WWW_FCODE_CN2 Implicit None3Integer, parameter:: MAX_PATH = +4Character (Len =MAX_PATH):: C5Character (Len = *), parameter:: Str_find ="RecordID:"6 integer:: ID, I, IERR7Open ( A, File ="Fcode.txt" )8 Do9Read ( A,'(a512)', IoStat = IERR) C!The format must be a512 or the space will be terminatedTen if(IErr/=0) Exit!//If you read the error, such as the end of the file, exit the loop One ifC1:2) =="//") Cycle!//If you are reading a comment line, read the next line directly Ai = index (c, str_find)!//Search for RecordID in C: Location - if(I >0) then!//If you find -i = i + Len (str_find)!//move position to RecordID: back thec = C (i:)!//So that the content of C is the content after I position, that is, discard the content before I position -Read (c, *) ID!//Read ID number -Write (c, *) ID + the!//ID number plus 2014, then write back to C. -c ="File" //Trim (Adjustl (c))//". Dat"! Combine into File2032.dat, pay attention to trim +Write (*, *) Trim (c)!//Output Look right -!//Open (File = Trim (c)) + Exit AEndif at End Do -Close ( A ) -End program WWW_FCODE_CN
Transferred from: http://fcode.cn/guide-48-1.html
[Reprint]:fortran string of stories