Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove SPACE

Status
Not open for further replies.

rafjak

Programmer
Nov 30, 2010
5
PL
Dear Collogues,

How to remove all SPACEs from the string,
wherever they are in the string.

Regards
Rafal

 
to remove leading and trailing spaces use intrinsic functions adjustl() and trim():
example trim_ex.f95
Code:
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]20[/color]) :: my_string
my_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'   Abc  '[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_string '"[/color], my_string, [COLOR=#ff00ff]"'"[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_string '"[/color], [COLOR=#008080]trim[/color]([COLOR=#008080]adjustl[/color](my_string)), [COLOR=#ff00ff]"'"[/color]
[COLOR=#a020f0]end[/color]
results in
Code:
$ g95 trim_ex.f95 -o trim_ex

$ trim_ex
 my_string '   Abc              '
 my_string 'Abc'
 
Thank You mikrom, but I know this functions
and those don't work on string like this:

'aaa bbb'

with different lengths of 'aaa' and 'bbb' strings.

Regards
Rafal
 
Hi rafjak

You must put up a DO loop, something like this:

Code:
     character*256 str1,str2
     integer*4 i,ls1,ls2

     ls1 = len_trim(str1)
     ls2 = 0
     do i = 1,ls1
        if(str1(i:i).ne.' ') then
           ls2 = ls2 + 1
           str2(ls2:ls2) = str1(i:i)
        endif
     enddo

!    The new string "str2" contains no spaces and has length "ls2"

     end
 
IT WORKS !!!!

Thank You very much

Best regards
Rafal
 
What I posted it's for "to remove leading and trailing spaces" and you have spaces middle in the string.

For that there isn't intrinsic function. Write you self one, for example something like this
Code:
[COLOR=#a020f0]program[/color] remove_spaces
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: my_string [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'  aaa bbb   cc   '[/color] 

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_string = '"[/color], my_string, [COLOR=#ff00ff]"'"[/color]

  [COLOR=#0000ff]! sweep blanks from string[/color]
  my_string [COLOR=#804040][b]=[/b][/color] sweep_blanks(my_string)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"my_string = '"[/color], [COLOR=#008080]trim[/color](my_string), [COLOR=#ff00ff]"'"[/color]

[COLOR=#a020f0]contains[/color] 
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) [COLOR=#a020f0]function[/color] sweep_blanks(in_str)
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: in_str
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: out_str
    [COLOR=#2e8b57][b]character[/b][/color] :: ch
    [COLOR=#2e8b57][b]integer[/b][/color] :: j

    out_str [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]" "[/color]
    [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#008080]len_trim[/color](in_str)
      [COLOR=#0000ff]! get j-th char[/color]
      ch [COLOR=#804040][b]=[/b][/color] in_str(j:j)
      [COLOR=#804040][b]if[/b][/color] (ch [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]" "[/color]) [COLOR=#804040][b]then[/b][/color]
        out_str [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]trim[/color](out_str) [COLOR=#804040][b]//[/b][/color] ch
      [COLOR=#804040][b]endif[/b][/color]
      sweep_blanks [COLOR=#804040][b]=[/b][/color] out_str 
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#a020f0]end function[/color] sweep_blanks

[COLOR=#a020f0]end program[/color] remove_spaces
Code:
$ g95 remove_spaces.f95 -o remove_spaces

$ remove_spaces
 my_string = '  aaa bbb   cc                '
 my_string = 'aaabbbcc'
 
I didn't see, that gullipe posted the solution before :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top