trim_quoted(3f) - [M_strings:WHITESPACE] converts regions of whitespace characters to a specified string (or nothing) (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
References
Author
License
function trim_quoted(STR,K) result (OUTSTR)
character(len=*),intent(in) :: STR character(len=*),intent(in),optional :: REP character(len=len(str)) :: OUTSTR
Whitespace is trimmed from both ends of the input string.
Text found between single and/or double quotes is not altered. A single quote is only considered a delimiter if preceded by a space or as the first non-blank character to allow for the use of a single quote in contractions.
Otherwise, trim_quoted(3) changes ranges of whitespace of various length found between words to a specified replacment string.
This would be similar to the sed(1) Basic Regular Expression
sed -i -e s/ */REP/gif no quoted regions were present.
STR input string whose whitespace regions are to be replaced REP string used to replace each region of whitespace. Defaults to a single blank.
OUTSTR Ouput string with all internal whitespace sections replaced by the replacement string removed
Sample Program:
program demo_trim_quoted use M_strings, only: trim_quoted implicit none character(len=*),parameter :: bracket=(*("[",g0,"]":,",")) character(len=*),parameter :: uno=(/,*(g0:,/)) character(len=:),allocatable :: a,ba = Esto es una prueba a ver como sale y determinar si & & realmente funciona bien la ruutina para eliminar blancos & & "intermedios de una hola cadena de caracteres" & & y ver ademas si "(respetamos las comillas) " & & realmente respeta las cadenas encerradas entre comillas. & &vamos a ver como sale este negocio. que mas puedo decir. & &probemos y veamos que pasa
print uno, Original tal y como se escribio (sin trim_quoted),a print uno, reducir espacios a uno 1, trim_quoted (a, ) print uno, reducir espacios a dos 2, trim_quoted (a, ) print uno, reducir espacios a cero 0, trim_quoted (a, )
a = "This is a test to see how it turns out and to determine if the& & routine to eliminate intermediate spaces from a text string & & really works well, and also to see if (we respect quotation & & marks) really respects strings enclosed in quotes. Lets see & & how this business turns out. What else can I say. Lets test and see& & what happens."
print uno, "Original exactly as it was written (without trim_quoted)",a print uno, reduce spaces to one 1, trim_quoted (a, ) print uno, reduce spaces to two 2, trim_quoted (a, ) print uno, reduce spaces to zero 0, trim_quoted (a, )
b = trim_quoted(a, ) print *, b
write(*,bracket)trim_quoted(this and that,)
write(*,bracket)trim_quoted( a b c ),a b c write(*,bracket)trim_quoted(a,xxxxx),a write(*,bracket)trim_quoted(,xxxxx), write(*,bracket)trim_quoted( a b c " dont touch " d e,:),& & a:b:c:" dont touch ":d:e write(*,bracket)trim_quoted( a ,xxxxx),a write(*,bracket)trim_quoted(" a quoted text ",--),& & "a-- quoted text " write(*,bracket)trim_quoted(" a quoted text abcd efg",--),& & "a-- quoted text --abcd--efg" end program demo_trim_quoted
Expected output
> Original tal y como se escribio (sin trim_quoted) > Esto es una prueba a ver como sale y determinar si > realmente funciona bien la ruutina para eliminar blanco > s "intermedios de una hola cadena de caract > eres" y ver ademas si "(respetamos las comil > las) " realmente respeta las cadenas encerradas en > tre comillas. vamos a ver como sale este negocio. que ma > s puedo decir. probemos y veamos que pasa > > reducir espacios a uno 1 > Esto es una prueba a ver como sale y determinar si realme > nte funciona bien la ruutina para eliminar blancos "inter > medios de una hola cadena de caracteres" y ve > r ademas si "(respetamos las comillas) " realme > nte respeta las cadenas encerradas entre comillas. vamos > a ver como sale este negocio. que mas puedo decir. probem > os y veamos que pasa > > reducir espacios a dos 2 > Esto es una prueba a ver como sale y determinar > si realmente funciona bien la ruutina para elimin > ar blancos "intermedios de una hola cadena > de caracteres" y ver ademas si "(respetamos la > s comillas) " realmente respeta las cadenas e > ncerradas entre comillas. vamos a ver como sale > este negocio. que mas puedo decir. probemos y ve > amos que pasa > > reducir espacios a cero 0 > Estoesunapruebaavercomosaleydeterminarsirealmentefunciona > bienlaruutinaparaeliminarblancos"intermedios de una > hola cadena de caracteres"yverademassi"(respetamos > las comillas) "realmenterespetalascadenasencerradas > entrecomillas.vamosavercomosaleestenegocio.quemaspuedodec > ir.probemosyveamosquepasa > > Original tal y como se escribio (sin trim_quoted) > This is a test to see how it turns out and to determin > e if the routine to eliminate intermediate spaces f > rom a text string really works well, and also to see i > f (we respect quotation marks) really respect > s strings enclosed in quotes. Lets see how this bus > iness turns out. What else can I say. Lets test and see > what happens. > > reduce spaces to one 1 > This is a test to see how it turns out and to determine i > f the routine to eliminate intermediate spaces from > a text string really works well, and also to see if (we > respect quotation marks) really respects strings > enclosed in quotes. Lets see how this business turns out > . What else can I say. Lets test and see what happens. > > reduce spaces to two 2 > This is a test to see how it turns out and to > determine if the routine to eliminate intermedia > te spaces from a text string really works well, > and also to see if (we respect quotation > marks) really respects strings enclosed in quote > s. Lets see how this business turns out. What > else can I say. Lets test and see what happens. > > reduce spaces to zero 0 > Thisisatesttoseehowitturnsoutandtodetermineiftheroutineto > eliminateintermediate spaces from a text stringrea > llyworkswell,andalsotoseeif(we respect quotation > marks)reallyrespectsstringsenclosedinquotes.Letsseehow > thisbusinessturnsout.WhatelsecanIsay.Letstestandseewhath > appens. > Thisisatesttoseehowitturnsoutandtodetermineiftheroutineto > eliminateintermediate spaces from a text stringrea > llyworkswell,andalsotoseeif(we respect quotation > marks)reallyrespectsstringsenclosedinquotes.Letsseehow > thisbusinessturnsout.WhatelsecanIsay.Letstestandseewhath > appens. > [thisandthat] > [a b c],[a b c] > [a],[a] > [],[] > [a:b:c:" dont touch ":d:e],[a:b:c:" dont touch ":d:e] > [a],[a] > [a-- quoted text ],[a-- quoted text ] > [a-- quoted text --abcd--efg],[a-- quoted text --abcd--efg]
Based on a contribution by Francisco Iglesias:https://fortran-lang.discourse.group/t/ sharing-a-classic-fortran-77 -utility-a-robust-string-trimming-function -with-quotes-protection-itrim/1097
o Francisco Iglesias (October-2024) o modified by John S. Urban for inclusion in M_strings(3) (June 2026), changed to use a replacement string instead of a specified number of blanks.
Public Domain
