• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

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

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 7.8k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    Pixero
    last edited by 10 Aug 2019, 12:15

    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?

    1 Reply Last reply Reply Quote 0
    • P Offline
      Pixero
      last edited by 10 Aug 2019, 12:27

      This seem to work:

      if e.definition.name.match(/^MyComp/)
      
      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 10 Aug 2019, 15:14

        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}/
        

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dan Rathbun
          last edited by 13 Sept 2019, 06:19

          .

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

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement