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")
Advertisement