Skip to main content
lathac41153227
Participating Frequently
May 14, 2018 5:47 AM UTC
Question

Check string contains values.

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");

This topic is closed to new replies. Start a new post to keep the conversation going.

14 replies

jacobtaylor
Participant
Jan 26, 2023 12:57 PM UTC
Here is a small function that checks the "word" with the "content" you'd like to search the word for.
 
function contains (word, content) {

for(var i = 0; i < (word.length-content.length+1); i++){
if(word.substring(i, i+content.length) == content) return true;
}

return false;
}
Translate
Fightergator
Inspiring
Jan 30, 2023 3:11 AM UTC

First, I appreciate the excellent tutorial on simple arrays and finding content in a string.  I have a related question.  In the last example, the "true" or "false" result appears in the JavaScript Console.  Is there something like a system variable that can be accessed outside of the function to use the result in something following the calling script; e.g.,

contains(word, content);

if (sysVarResult = true) {
// do something
}

I currently define a boolean variable, pass it to the function, assign the result, and pass it back, which seems clumsy to me. Does this make sense?

Translate
Klaus Göbel
Legend
Jan 30, 2023 7:32 AM UTC

It is very simple. You just only have to return the result of the loop:

    var topPageArray=[1,2,3,4,5,6,10,11];

    var FindValue = 5;
    var gbFound = false
    
    var result = contains(FindValue, topPageArray);
    
    alert(result);

function contains(fValue, fArray)
{
    var FindPos = -1;
    var bFound = false;
    
    for (var i = 0; i < fArray.length; i++)
        {
        if (FindValue == fArray[i])
            {
            FindPos = i;
            bFound = true;
            break;
            }
        }
    
    return bFound;
}
Translate
Ian Proudfoot
Legend
May 14, 2018 10:58 AM UTC

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

Translate
lathac41153227
Participating Frequently
May 14, 2018 12:09 PM UTC

Thank you for your response.

Translate
Klaus Göbel
Legend
May 14, 2018 5:49 AM UTC

Thats mere Javascript: indexOf

JavaScript String indexOf() Method

Translate
lathac41153227
Participating Frequently
May 14, 2018 6:17 AM UTC

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.

Translate
Klaus Göbel
Legend
May 14, 2018 6:42 AM UTC

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)

        {

        FindPos = i;

        break;

        }

    }

if (FindPos >= 0)

    {

    alert("found at Position: " + FindPos);

    }

else

    {

    alert("Not found");

    }

Translate