sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    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 3 Watching
    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.
    • PixeroP Offline
      Pixero
      last edited by

      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
      • PixeroP Offline
        Pixero
        last edited by

        This seem to work:

        if e.definition.name.match(/^MyComp/)
        
        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          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
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            .

            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