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 #53336 is a reply to message #53240] Mon, 02 April 2007 14:38 Go to previous message
JD Smith is currently offline  JD Smith
Messages: 850
Registered: December 1999
Senior Member
On Mon, 02 Apr 2007 14:21:42 -0700, rchughes 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.

For a fixed length string like this, it's easy:

print,strmid(string_array,4)

But, suppose it's a non-uniform length string:

string_array = ['dog.spot','cat.betsy','doggy.rover','parrot.bill','dog.luc ky']
p=strpos(string_array,'.')
print,strmid(string_array,1#p+1)

Notice that silly use of 1#p. This is to ensure that only 1
sub-string is extracted from each string in the array (try it without
the 1# and you'll see what I mean; see STRMID docs).

Or you could even be more general and use a regular expression:

string_array = ['dog.spot','cat*betsy','doggy#rover','parrot&bill','dog @lucky']
p=stregex(string_array,'^.+[.*#&@]',LENGTH=len)
print,strmid(string_array,1#len)

JD
[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: Sat Oct 11 10:28:28 PDT 2025

Total time taken to generate the page: 0.00199 seconds