comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Removing some but not all spaces in a string
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Removing some but not all spaces in a string [message #61764 is a reply to message #61758] Mon, 28 July 2008 19:41 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
wlandsman <wlandsman@gmail.com> writes:

> I'm looking for suggestions for the best way remove the spaces around
> "operators" (e.g. <. > , =) in a string but not around other
> characters. For example, I want the string
>
> 'name = ngc 5548, v <15'
>
> to become
>
> 'name=ngc 5548, v<15'

I usually do specialized string twizzling at the byte level, which is
done in TEST2 below. I'm not sure if TEST2 faster or more elegant
than your TEST, but it avoids all the gluing and ungluing inherent in
the STREGEX() method. It's a pity there isn't a STREPLACE() which
handles regular expressions.

I also attached a version based on REPSTR() but since that sub-routine
itself does glue/unglue, it's just hiding the mess behind a facade.

Craig


pro test2, st
space = 32b
ops = '<>=' ;; Operations to delete spaces
bops = byte(ops)

st = strtrim(strcompress(st),2)
if st EQ '' then return

bb = byte(st)
mask = 0b
for i = 0, n_elements(bops)-1 do begin
mask = mask OR [(bb EQ space AND bb(1:*) EQ bops(i)),0b]
mask = mask OR [0b,(bb EQ bops(i) AND bb(1:*) EQ space)]
endfor

wh = where(mask EQ 0)
st = string(byte(bb(wh)))
return
end

pro test3, st
space = ' '
ops = ['<','>','='] ;; Operations to delete spaces

st = strtrim(strcompress(st),2)
if st EQ '' then return
for i = 0, n_elements(ops)-1 do begin
op = ops(i)
st = repstr(st, ' '+op, op)
st = repstr(st, op+' ', op)
endfor
return
end


--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Array has a corrupted discriptor, reliable?
Next Topic: error on stddev estimate

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 11:23:27 PDT 2025

Total time taken to generate the page: 0.96192 seconds