CHARACTER(3f) - [M_unicode:CONVERSION] convert type(unicode_type) string to a CHARACTER variable (LICENSE:MIT)
Synopsis
Characteristics
Description
Options
Result
Examples
See Also
Author
License
result = character(STRING,start,end,inc) or result = STRING%character(start,end,inc)
elemental function character(string,start,end,inc)type(unicode_type),intent(in) :: string integer,intent(in) :: start integer,intent(in) :: end integer,intent(in) :: inc
o STRING is a scalar or array string variable o the returned value is a CHARACTER scalar or array
CHARACTER(3f) returns a CHARACTER variable given a string variable of type type(unicode_type).
o STRING : A scalar or array string to convert to intrinsic CHARACTER type.
The result converts each string to bytes stored in CHARACTER variables. All elements will be padded to the same length of the longest element; as all elements of a CHARACTER array are required to be of the same length.Commonly used to pass data to procedures requiring CHARACTER variables or for printing when the DT format is not used..
Sample program
program demo_character use M_unicode, only : ut=>unicode_type, ch=>character, trim, len, pad use M_unicode, only : write(formatted), assignment(=) type(ut) :: ustr type(ut),allocatable :: array(:) integer :: i character(len=*),parameter :: all=(*(g0))Results:ustr=[949, 8021, 961, 951, 954, 945, 33] ! eureka in codepoints ! when doing I/O using DT might be the most intuitive ! but sometimes converting to intrinsic character variables ! is preferred write (*,all) ch(ustr) ! convert to CHARACTER variable write (*,all) ustr%character() ! convert to CHARACTER variable ! you can select a range of glyphs write (*,all) ustr%character(3,4) ! similar to LINE(3:4) for ! CHARACTER variables ! and even reverse a string write (*,all) ustr%character(len(ustr),1,-1) ! reverse string ! note that OOP syntax provides a few other options write (*,all) ustr%byte() ! convert to CHARACTER(LEN=1) type
! arrays ! ! using this syntax make sure to make the LEN value large enough ! that glyphs can take up to four bytes array= ut([ character(len=60) :: & Confucius never claimed to be a prophet, ,& but I think he foresaw AI! He said ,& ,& "学而不思则罔,思而不学则殆" ,& or ,& (xué ér bù sī zé wǎng, sī ér bù xué zé dài), ,& which is also ,& "To learn without thinking is to be lost, ,& to think without learning is to be in danger".]) ! write(*,(*(:,"[",g0,"]",/)))ch(array) ! all elements will be the same length in bytes but not necessarily !in glyphs write(*,(a,*(i0,1x)))all elements the same length in BYTES:, & & len(ch(array)) write(*,(a,*(i0,1x)))lengths (in glyphs):,len(array) array=trim(array) write(*,(a,*(i0,1x)))lengths after trimming (in glyphs):, & & len(array) write(*,(:*(:,"[",g0,"]",/)))ch(array) write(*,*) ! ! using this syntax the elements will be of different lengths array= [ & ut(Confucius never claimed to be a prophet,) ,& ut(but I think he foresaw AI! He said) ,& ut() ,& ut( "学而不思则罔,思而不学则殆") ,& ut(or) ,& ut( (xué ér bù sī zé wǎng, sī ér bù xué zé dài),) ,& ut(which is also) ,& ut( "To learn without thinking is to be lost,) ,& ut( to think without learning is to be in danger".)] ! but using the CHARACTER function will still make them the same ! length in bytes so you might want to print them individually ! for certain effects, subject to font properties such as varying ! glyph widths. write(*,(*("[",g0,"]",/)))(ch(array(i)),i=1,size(array)) write(*,(*("[",g0,"]",/)))(ch(pad(array(i),60)),i=1,size(array)) ! end program demo_character
> εὕρηκα! > εὕρηκα! > ρη > !ακηρὕε > εὕρηκα! > [Confucius never claimed to be a prophet, ] > [but I think he foresaw AI! He said ] > [ ] > [ "学而不思则罔,思而不学则殆" ] > [or ] > [ (xué ér bù sī zé wǎng, sī ér bù xué zé dài), ] > [which is also ] > [ "To learn without thinking is to be lost, ] > [ to think without learning is to be in danger". ] > > all elements the same length in BYTES:60 > lengths (in glyphs):60 60 60 34 60 48 60 60 60 > lengths after trimming (in glyphs):40 34 0 16 2 45 13 42 47 > [Confucius never claimed to be a prophet, ] > [but I think he foresaw AI! He said ] > [ ] > [ "学而不思则罔,思而不学则殆" ] > [or ] > [ (xué ér bù sī zé wǎng, sī ér bù xué zé dài),] > [which is also ] > [ "To learn without thinking is to be lost, ] > [ to think without learning is to be in danger". ] > > > [Confucius never claimed to be a prophet,] > [but I think he foresaw AI! He said] > [] > [ "学而不思则罔,思而不学则殆"] > [or] > [ (xué ér bù sī zé wǎng, sī ér bù xué zé dài),] > [which is also] > [ "To learn without thinking is to be lost,] > [ to think without learning is to be in danger".] > [ > [Confucius never claimed to be a prophet, ] > [but I think he foresaw AI! He said ] > [ ] > ["学而不思则罔,思而不学则殆" ] > [or ] > [(xué ér bù sī zé wǎng, sī ér bù xué zé dài), ] > [which is also ] > ["To learn without thinking is to be lost, ] > [to think without learning is to be in danger". ] > [
functions that perform operations on character strings:
o elemental: adjustl(3), adjustr(3), index(3), scan(3), verify(3) o non-elemental: len_trim(3), len(3), repeat(3), trim(3)
John S. Urban
