From: Jason Voegele Date: 2005-07-29T21:50:50+09:00 Subject: Re: Chopping the beginning of a string elegantly On Fri, July 29, 2005 8:41 am, francisrammeloo@hotmail.com said: > Suppose I want to chop off the "Begin" in the string "BeginCaption". > My solution is this: > > thePane = widgetdescription.scan( /Begin\w+/ )[0] > thePane = widgetdescription.["Begin".length..thePane.length] > > Personally I don't find this method very elegant. > > Can anybody offer me some hints on how to make this code more > presentable? Use the sub method: widget_description = "BeginCaption" the_pane = widget_description.sub(/^Begin/, "") The '^' in the regular expression above is an anchor to the beginning of the string. > Also perhaps a similar strategy if I would want to remove something > from the middle or the end? Just leave the '^' out of the regular expression from above. If you want to remove at the end, put a '$' at the end of your regular expression. -- Jason Voegele "There is an essential core at the center of each man and woman that remains unaltered no matter how life's externals may be transformed or recombined. But it's smaller than we think." -- Gene Wolfe, The Book of the Long Sun