Forum
    • Categories
    • Register
    • Login

    How to get numbers from strings

    Scheduled Pinned Locked Moved General Questions
    6 Posts 6 Posters 42 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.
    • ulrikU
      ulrik
      last edited by

      I have a function that iterate over strings like

       strings = ["brass", "001", "127", "flute"]
      

      How can I find the string numbers from this array?
      I have tried "typeof", of course that's not going to work, eval(), etc....
      I even tried to let all strings pass

      Engine.getMidiNoteName(string)
      

      and it log "0" for all letter strings but it works for the "number" strings
      How do you do it?

      Hise Develop branch
      MacOs 15.6.1, Xcode 16.2
      http://musikboden.se

      Oli UllmannO ustkU dannytaurusD David HealeyD 4 Replies Last reply Reply Quote 0
      • Oli UllmannO
        Oli Ullmann @ulrik
        last edited by Oli Ullmann

        @ulrik
        I'm not sure I fully understand your question, but there is String.getIntValue(). This function converts a string into an integer.

        And there is Engine.getValueForText(String text, String convertedMode).

        1 Reply Last reply Reply Quote 0
        • ustkU
          ustk @ulrik
          last edited by ustk

          Use parseInt. But you need to check the charcode of the first character to get rid of letter parsing returning 0

          const var strings = ["brass", "001", "127", "flute"];
          
          for (s in strings)
          {
          	if (s.charCodeAt(0) >= 48 && s.charCodeAt(0) <= 57) // <= 0-9 numbers only
          		Console.print(parseInt(s));
          }
          

          As @Oli-Ullmann said String.getIntValue() also works, I don't know if there's any difference between the two...

          Hise made me an F5 dude, any other app just suffers...

          1 Reply Last reply Reply Quote 2
          • dannytaurusD
            dannytaurus @ulrik
            last edited by dannytaurus

            @ulrik Or use regex to detect strings that only contains digits.

            const var strings = ["brass", "001", "127", "flute"];
            
            for (s in strings)
            {
              if (Engine.matchesRegex(s, "^\\d+$")) Console.print(parseInt(s));
            }
            

            ^ means 'start of string'
            \\d means digit
            + means 'one or more'
            $ means 'end of string`

            So, "one or more digits between the start and end of the string, and nothing else"

            Meat Beats: https://meatbeats.com
            Klippr Video: https://klippr.video

            1 Reply Last reply Reply Quote 2
            • David HealeyD
              David Healey @ulrik
              last edited by

              @ulrik said in How to get numbers from strings:

              How can I find the string numbers from this array?

              How did the numbers get into the array as strings?

              Free HISE Bootcamp Full Course for beginners.
              YouTube Channel - Public HISE tutorials
              My Patreon - HISE tutorials

              1 Reply Last reply Reply Quote 1
              • OrvillainO
                Orvillain
                last edited by

                @ulrik are you parsing dodgy data from somewhere?? Because grouping numbers and text strings like that into a single array seems like a poor design.

                Musician - Instrument Designer - Sonic Architect - Creative Product Owner
                Crafting sound at every level. From strings to signal paths, samples to systems.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post

                37

                Online

                2.2k

                Users

                13.4k

                Topics

                116.2k

                Posts