Appearance
2. Cleaning and transforming
Text from a file or from a user is rarely clean. The header of the table of report arrives with blanks everywhere.
Blanks
f90
header = ' year, make , model,price '
print '(A)', '['//header//']'
header = header%strip() ! blanks at both ends
print '(A)', '['//header//']'
header = header%replace(old=' ', new='') ! every blank
print '(A)', '['//header//']'
print '(A)', '['//header%upper()//']'stripremoves the blanks at both ends.replacesubstitutes every occurrence ofoldwithnew; an emptynewdeletes.upper(andlower) change the case.
Words
f90
note = 'a small city car'
note = note%unique(' ') ! runs of blanks collapsed to one
print '(A)', note%startcase()//''
print '(A)', note%snakecase()//''
print '(A)', note%camelcase()//''
print '(A)', note%capitalize()//''unique collapses every run of a repeated substring to one occurrence. startcase, snakecase and camelcase split the string in words and rewrite them in a style; capitalize makes upper case the first character only.
A set of characters
f90
note = '** sold **'
print '(A)', '['//note%strip(remove='* ')//']' ! a set of characters to removeWith remove, strip removes from both ends every character of a set, in any order they occur.
Whitespace, repeats, character sets
f90
note = ' ac'//achar(9)//'abs moon '
print '(A)', '['//note%compact()//']' ! whitespace runs to one blank, the ends removed
note = 'extended edition!!! ...'
print '(A)', note%squeeze(set='!.')//'' ! runs of these characters reduced to one
note = 'E350'
print '(A)', note%transliterate('0123456789', '#')//'' ! every digit becomes a '#'compactturns every run of whitespace, blanks, tabs or new lines, into one blank and removes it at both ends.squeezereduces every run of a repeated character to one; withset, only for the characters of the set.transliteratereplaces each character of a set by the one at the same position in another set; a shorter second set repeats its last character, an empty one deletes.
Running it
$ report
[ year, make , model,price ]
[year, make , model,price]
[year,make,model,price]
[YEAR,MAKE,MODEL,PRICE]
A Small City Car
a_small_city_car
ASmallCityCar
A small city car
[sold]
[ac abs moon]
extended edition! .
E###The whole program:
f90
program report
!< Tutorial, chapter 2: cleaning and transforming.
use stringifor
implicit none
type(string) :: header, note
header = ' year, make , model,price '
print '(A)', '['//header//']'
header = header%strip() ! blanks at both ends
print '(A)', '['//header//']'
header = header%replace(old=' ', new='') ! every blank
print '(A)', '['//header//']'
print '(A)', '['//header%upper()//']'
note = 'a small city car'
note = note%unique(' ') ! runs of blanks collapsed to one
print '(A)', note%startcase()//''
print '(A)', note%snakecase()//''
print '(A)', note%camelcase()//''
print '(A)', note%capitalize()//''
note = '** sold **'
print '(A)', '['//note%strip(remove='* ')//']' ! a set of characters to remove
note = ' ac'//achar(9)//'abs moon '
print '(A)', '['//note%compact()//']' ! whitespace runs to one blank, the ends removed
note = 'extended edition!!! ...'
print '(A)', note%squeeze(set='!.')//'' ! runs of these characters reduced to one
note = 'E350'
print '(A)', note%transliterate('0123456789', '#')//'' ! every digit becomes a '#'
endprogram reportWhat you learned
strip, replace, unique, the case methods; compact, squeeze, transliterate. Each returns a new string: assign it back to change the variable. Reference: String Manipulation.
Next: 3. Splitting and joining.