Skip to main content
lathac41153227
Participating Frequently
May 14, 2018
Question

Check string contains values.

  • May 14, 2018
  • 3 replies
  • 14157 views

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 has been closed for replies.

3 replies

jacobtaylor
Participant
January 26, 2023
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;
}
Fightergator
Inspiring
January 30, 2023

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?

Klaus Göbel
Legend
January 30, 2023

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;
}
Ian Proudfoot
Legend
May 14, 2018

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

lathac41153227
Participating Frequently
May 14, 2018

Thank you for your response.

Klaus Göbel
Legend
May 14, 2018

Thats mere Javascript: indexOf

JavaScript String indexOf() Method

lathac41153227
Participating Frequently
May 14, 2018

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.

Klaus Göbel
Legend
May 14, 2018

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

    }