8 Replies Latest reply on May 14, 2018 5:51 AM by Ian Proudfoot

    Check string contains values.

    lathac41153227

      Hi,

       

      How to check whether a string contains the specified values in extendscript for framemaker ?How to use in following code using extendscript for    framemaker ?

          var str = "Hello world, welcome to the universe.";

          var n = str.includes("world");

        • 2. Re: Check string contains values.
          lathac41153227 Level 1

          Hi,

          I have used in this code,i need to check whether a string contains values in extendscript for framemaker , not string contains values position,"indexOf" used in values position from string.So please suggests if any ideas.

          var topPage="1,2,3,4"

          var tempValue="5"

          if (topPage.indexOf(tempValue + ",") != -1) 

          {

               alert("true");

          }

          else

          {

               alert("false");

            }

           

           

          Thank you.

          • 3. Re: Check string contains values.
            Klaus Göbel Level 3

            You asked for STRINGS!!!!

             

            If you want to find numbers, you better use arrays.

            In your code you'll never find the last number (4),

             

            var topPageArray=[1,2,3,4,5,6,10,11];
            var FindValue = 5;
            var FindPos = -1;
            for (var i = 0; i < topPageArray.length; i++)
                {
                if (FindValue == topPageArray[i])
                    {
                    FindPos = i;
                    break;
                    }
                }
            if (FindPos >= 0)
                {
                alert("found at Position: " + FindPos);
                }
            else
                {
                alert("Not found");
                }
            
            • 4. Re: Check string contains values.
              lathac41153227 Level 1

              Hi,

              Thank you for your response.In your code used in find position of values.but i need if string contains values or not.For example

              var topPage="1,2,3,4"

              var tempValue="2,"

              if (topPage.indexOf(tempValue + ",") != -1)

              {

                   alert("true");

              }

              else

              {

                   alert("false");

                }

               

              If topPage having tempValue or not , find tempValue "2," in topPage or not, find tempValue "5," in topPage or not ? how to check using extendscript for framemaker,Please suggests if any ideas.

               

              Thank you.

              • 5. Re: Check string contains values.
                Ian Proudfoot Adobe Community Professional

                Hi

                While Klaus is correct that you are working with integers, they arrive at your code as a single string, so it's a valid question. There are at least three ways to check:

                • String.indexOf(searchValueString, offsetInteger) returns an index integer or -1 if the string isn't found.
                • String.Matches(searchValueRegex) returns an array including all matches or null if not found.
                • String.Search(searchValueRegex) returns an index integer or -1 if the string isn't found.

                 

                Your previous post is almost correct:

                function contains() {
                    var topPage="1,2,3,4,2"
                    var tempValue="2"
                    if (topPage.indexOf(tempValue) !== -1) 
                         {
                         alert("true");
                         }
                    else
                         {
                         alert("false");
                         }
                    }
                

                 

                The only change was to remove the trailing comma from temp value - your comma delimited string does not end with a comma, so it could never find the last value.

                The value checked by topPage.indexOf() does not need the added comma. The way it was originally written looks for '2,,'.

                 

                Also to reinforce what Klaus said, if you have control over the way the data is handled you may find it easier to store lists of values in an array.

                 

                I hope that helps?

                Ian

                1 person found this helpful
                • 6. Re: Check string contains values.
                  lathac41153227 Level 1

                  Thank you for your response.

                  • 7. Re: Check string contains values.
                    Klaus Göbel Level 3

                    And here is, how you can transform your comma separated string into an array:

                    var topPageArray = topPage.split(","); // converts a string into an array with separator ",".
                    

                     

                    and here, how you can build your array from scratch

                    var topPageArray = [];
                    topPageArray.push(Pagenumber); // put it in a loop. "push() adds a value at the end of the array
                    
                    • 8. Re: Check string contains values.
                      Ian Proudfoot Adobe Community Professional

                      Good points Klaus. Of course you can also treat a text string as an array. In that case it would be an array of single characters:

                      arr('My text string');
                      
                      function arr(text){
                          var myChar=text[3];
                          var howLong=text.length;
                          }
                      

                       

                      Ian

                      1 person found this helpful