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

Problem with removing letters from string

Scheduled Pinned Locked Moved Developers' Forum
10 Posts 5 Posters 369 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.
  • R Offline
    renderiza
    last edited by renderiza 17 Sept 2013, 18:43

    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
    • R Offline
      renderiza
      last edited by 17 Sept 2013, 18:59

      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 17 Sept 2013, 21:31

        @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
        • D Offline
          Dan Rathbun
          last edited by 18 Sept 2013, 00:19

          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
          • R Offline
            renderiza
            last edited by 18 Sept 2013, 11:26

            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
            • T Offline
              tt_su
              last edited by 18 Sept 2013, 11:31

              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
              • R Offline
                renderiza
                last edited by 18 Sept 2013, 12:33

                @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
                • T Offline
                  TIG Moderator
                  last edited by 22 Sept 2013, 12:56

                  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
                  • T Offline
                    tt_su
                    last edited by 22 Sept 2013, 18:09

                    @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
                    • R Offline
                      renderiza
                      last edited by 24 Sept 2013, 01:01

                      @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
                      1 / 1
                      • First post
                        1/10
                        Last post
                      Buy SketchPlus
                      Buy SUbD
                      Buy WrapR
                      Buy eBook
                      Buy Modelur
                      Buy Vertex Tools
                      Buy SketchCuisine
                      Buy FormFonts

                      Advertisement