Highlighted

Check string contains values.

Community Beginner ,
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

5.4K

Likes

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

Check string contains values.

Community Beginner ,
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

5.4K

Likes

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
May 13, 2018 0
Enthusiast ,
May 13, 2018

Copy link to clipboard

Copied

Thats mere Javascript: indexOf

JavaScript String indexOf() Method

Likes

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
Reply
Loading...
May 13, 2018 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
May 13, 2018 0
Enthusiast ,
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");

    }

Likes

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
Reply
Loading...
May 13, 2018 0
Community Beginner ,
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.

Likes

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
Reply
Loading...
May 14, 2018 0
Enthusiast ,
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

Likes

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
Reply
Loading...
May 14, 2018 2
Community Beginner ,
May 14, 2018

Copy link to clipboard

Copied

Thank you for your response.

Likes

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
Reply
Loading...
May 14, 2018 0
Enthusiast ,
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

Likes

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
Reply
Loading...
May 14, 2018 1
Enthusiast ,
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

Likes

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
Reply
Loading...
May 14, 2018 2