• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Check string contains values.

Community Beginner ,
May 13, 2018 May 13, 2018

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

12.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 13, 2018 May 13, 2018

Copy link to clipboard

Copied

Thats mere Javascript: indexOf

JavaScript String indexOf() Method

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 13, 2018 May 13, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 13, 2018 May 13, 2018

Copy link to clipboard

Copied

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

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

Thank you for your response.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 14, 2018 May 14, 2018

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

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;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 29, 2023 Jan 29, 2023

Copy link to clipboard

Copied

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;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

Yes, just used the function as the bool.

 

if (contains(word, content)) {

     // do something

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

@jacobtaylor  I think that it is easier for beginners to see single steps. But your solution is of course much more compact - thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 30, 2023 Jan 30, 2023

Copy link to clipboard

Copied

LATEST

Both solutions are more streamline than what I was doing, and I will add both to my knowledge base.  Klaus, your use of the function as the bool explains similar code snippets I've seen in the past.  I couldn't understand them until now.  My thanks to both of you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines