How do I check if component name starts with a certain word?
-
I noticed a bug in one of my scripts because there can be multiple components created with names like MyComp, MyComp#1, MyComp#2 and so on.
Is there a way of checking what a name starts with?
Instead of:
if e.definition.name == "MyComp"I need something like:
if e.definition.name STARTS WITH? "MyComp" (Pseudo code)Is that possible?
-
This seem to work:
if e.definition.name.match(/^MyComp/) -
Another way of phrasing it is:
if e.definition.name =~ /^MyComp/To pass a 'variable' instead of a string of characters, use
patt = "MyComp" ### or whatever you want if e.definition.name =~ /^#{patt}/ -
.
Ruby 2.x+ (SketchUp 2014+)
[String#start_with?](https://ruby-doc.org/core-2.0.0/String.html#method-i-start_with-3F)ie ...
if e.definition.name.start_with?("MyComp")... or ...
if e.definition.name.start_with?("Door","Window")
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement