How do I check if component name starts with a certain word?

How do I check if component name starts with a certain word?

Postby Pixero » Sat Aug 10, 2019 1:15 pm

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:
Code: Select all
if e.definition.name == "MyComp"


I need something like:
Code: Select all
if e.definition.name STARTS WITH? "MyComp" (Pseudo code)


Is that possible?
0
User avatar
Pixero 
 

Re: How do I check if component name starts with a certain w

Postby Pixero » Sat Aug 10, 2019 1:27 pm

This seem to work:
Code: Select all
if e.definition.name.match(/^MyComp/)
0
User avatar
Pixero 
 

Re: How do I check if component name starts with a certain w

Postby TIG » Sat Aug 10, 2019 4:14 pm

Another way of phrasing it is:
Code: Select all
if e.definition.name =~ /^MyComp/


To pass a 'variable' instead of a string of characters, use
Code: Select all
patt = "MyComp" ### or whatever you want
if e.definition.name =~ /^#{patt}/
1
TIG
User avatar
TIG 
Global Moderator
 

Re: How do I check if component name starts with a certain w

Postby Dan Rathbun » Fri Sep 13, 2019 7:19 am

.

Ruby 2.x+ (SketchUp 2014+)

String#start_with?

ie ...

Code: Select all
if e.definition.name.start_with?("MyComp")


... or ...

Code: Select all
if e.definition.name.start_with?("Door","Window")
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    SketchUcation One-Liner Adverts

    by Ad Machine » 5 minutes ago



    Ad Machine 
    Robot
     



     

    Return to Developers' Forum

    Who is online

    Users browsing this forum: formchrissy and 9 guests

    Visit our sponsors: