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!
    ⚠️ Important | Libfredo 15.6b introduces important bugfixes for Fredo's Extensions Update

    Problem with removing letters from string

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 5 Posters 454 Views 5 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.
    • renderizaR Offline
      renderiza
      last edited by renderiza

      How can I remove the first four letters of a string?

      Here is a test that is giving me strange result...

      a = "hello there"
      a[1] #=> "101"

      I was expecting this...

      a = "hello there"
      a[1] #=> "e"

      What I am doing wrong?

      [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

      1 Reply Last reply Reply Quote 0
      • renderizaR Offline
        renderiza
        last edited by

        This one seems to work.

        a = "hello there"
        a[1,1] #=> "e"

        [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

        1 Reply Last reply Reply Quote 0
        • S Offline
          slbaumgartner
          last edited by

          @renderiza said:

          How can I remove the first four letters of a string?

          Here is a test that is giving me strange result...

          a = "hello there"
          a[1] #=> "101"

          I was expecting this...

          a = "hello there"
          a[1] #=> "e"

          What I am doing wrong?

          a[1] returns the character in the second position of the string, and in the version of Ruby embedded in SketchUp a character is displayed as its numeric value, not its ASCII representation. You can use a[1].chr to get a string containing the ASCII.

          In your second example, a[1,1] is a length 1 slice from the string, which is also a string (not a character) so it displays as a string.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            Be careful, ... in Ruby 1.9+ they changed the [ ] method to return what we all expect an index to return.

            try using a range:
            a = "hello there" b = a[4..-1] %(#804000)[>> "o there"]

            💭

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • renderizaR Offline
              renderiza
              last edited by

              thank you guys!

              Here is what I ended up using to eliminate first four letters...

              "sting"[4,100] #=> "g"

              [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

              1 Reply Last reply Reply Quote 0
              • tt_suT Offline
                tt_su
                last edited by

                I would recommend you use Dan suggestions as it's always correct. You example will fail when you feed it a string that's over 100 characters. And it's not a good practice to give out of range values.

                1 Reply Last reply Reply Quote 0
                • renderizaR Offline
                  renderiza
                  last edited by

                  @tt_su said:

                  I would recommend you use Dan suggestions as it's always correct. You example will fail when you feed it a string that's over 100 characters. And it's not a good practice to give out of range values.

                  Will replace the code to Dan is suggestion for the next update of "Rename by Layer" plugin.

                  Thanks! 👍

                  [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                  1 Reply Last reply Reply Quote 0
                  • TIGT Offline
                    TIG Moderator
                    last edited by

                    BUT be aware that non-ASCII accented characters etc are actually 'two-bits'...
                    So then
                    **a = "héllo there"** héllo there **b = a[4..-1]** lo there

                    BUT all is not lost:
                    **a = "hello there" b = a.split('')** ["h", "e", "l", "l", "o", " ", "t", "h", "e", "r", "e"]
                    AND
                    **a = "héllo there" b = a.split('')** ["h", "é", "l", "l", "o", " ", "t", "h", "e", "r", "e"]
                    SO THEN
                    **b = a.split('')[4..-1].join('')** o there
                    should do it, even with strings containing non-ASCII characters 💭

                    TIG

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by

                      @tig said:

                      should do it, even with strings containing non-ASCII characters 💭

                      Interesting - split uses regex functions, which is to an extent in Ruby 1.8 aware of Unicode.

                      1 Reply Last reply Reply Quote 0
                      • renderizaR Offline
                        renderiza
                        last edited by

                        @tig said:

                        SO THEN
                        **b = a.split('')[4..-1].join('')** o there
                        should do it, even with strings containing non-ASCII characters 💭

                        Great advice!...thank you guys! 👍

                        [url=https://www.sketchupcode.com/:z3kqsidd]My Extensions ...[/url:z3kqsidd]

                        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