5FT-20Designs Posted September 25, 2022 Share Posted September 25, 2022 I started adding abbreviations to the beginning of my CAD Details, Cameras and Sections in an attempt to keep some sort of order. For example “SC##” are schedules, “DF##” are foundation details and so on. When I send a CAD detail, Camera or Section, I use “slice” to remove the first 6 characters from the name. I would like to eliminate the 6 character limit and search for “- “ to determine how many to strip. This will allow me use any number of characters I need. I’ve tried the following and can get the “index” to return the correct number, but the “slice” doesn’t evaluate. Can this be done as I have above? Any suggestions would be greatly appreciated. Thanks, Keith Link to comment Share on other sites More sharing options...
5FT-20Designs Posted September 25, 2022 Author Share Posted September 25, 2022 Eric, Thank you for the help. I used gsub!. Keith Link to comment Share on other sites More sharing options...
Alaskan_Son Posted September 26, 2022 Share Posted September 26, 2022 A few notes: On 9/24/2022 at 7:00 PM, 5FT-20Designs said: I’ve tried the following and can get the “index” to return the correct number, but the “slice” doesn’t evaluate. Here's what you typed up... %automatic_label.Slice!(%automatic_label.index("-")+2..60% This would have actually worked just fine except that you had a number of small mistakes. What you should have typed was.... %automatic_label.slice!((automatic_label.index("-")+2)..60)% With corrections marked... %automatic_label.slice!((%automatic_label.index("-")+2)..60)% Having said that, and although it will work, I would suggest a few other things about this approach... Using slice! instead of simply slice isn't really necessary in this situation. It's not hurting anything, but it serves no purpose in the context in which you're using it. To take it one step further though... You don't even need to use slice at all. The string[index] method will actually do the same thing. Using an extra large index (60) to make sure you grab all possible characters is a sloppy way of doing so. You should really use -1 which represents the last character in the string. So a cleaned up version of your same general approach with changes would be... %automatic_label[(automatic_label.index("-")+2)..-1]% Anyway, as Eric pointed out, there are MANY ways of getting what you want, but I would suggest something I find to be a bit more succinct and explicit. That is, using partition(separator)... %automatic_label.partition(" - ")[2]% This method very deliberately breaks your string into an array containing 3 parts: a prefix, your defined separator, and a suffix. You then simply specify inside brackets which of the 3 elements you want. Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now