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

Home » Public Forums » archive » removing substring from string array
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 substring from string array [message #53333 is a reply to message #53240] Mon, 02 April 2007 14:40 Go to previous messageGo to previous message
Jean H. is currently offline  Jean H.
Messages: 472
Registered: July 2006
Senior Member
rchughes@gmail.com wrote:
> Hi All,
>
> Does anyone know a quick way to remove the same prefix from all
> elements in a string array?
>
> Here is an example:
> start with string_array = ['dog.spot', 'dog.rover', 'dog.lucky']
>
> and get ['spot', 'rover', 'lucky']
>
> The only way I know I can do this is with a FOR loop and the
> STRSPLIT(string_array[i], '.', /EXTRACT) command. I am hoping someone
> could help me out with a much more efficient way if there is one.
>
> Thanks in advance,
> Ryan.
>

Hi,

If you know that every element in the array starts with the prefix, you
can do something like
prefixLength = strlen(prefix)
stringWithoutPrefix = strmid(stringArray, prefixLength)

If some entries may NOT start with the prefix, you can extract the
prefix, verify it and if it is the good one, remove it

prefix_array = strmid(stringArray, 0, prefixLength)
entriesWithThePrefix = where(prefix_array eq rebin(prefix,
n_elements(stringArray))

stringWithoutPrefix = strmid(stringArray[entriesWithThePrefix],
prefixLength)


You might want to do something to keep the entries that do not start
with the prefix

Hope that helps,
Jean
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: rotate and transpose images in xobjviewer
Next Topic: General help

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

Current Time: Thu Oct 09 22:34:22 PDT 2025

Total time taken to generate the page: 6.95922 seconds