Home Forums General XMetaL Discussion how to check if an attribute list contains a string. Reply To: how to check if an attribute list contains a string.

Derek Read

Reply to: how to check if an attribute list contains a string.

XMetaL doesn't expose any APIs for string manipulation. However, there is no need as all scripting languages have a string object of some kind with associated methods and properties.

Please have a look at Microsoft's documentation for the Windows JScript engine and Windows Script Host. Most likely the version you have installed will be 5.7, 5.8 or possibly 9. Microsoft has an entire section devoted to each object, including their String object. There are also a lot of books out there that are very good at explaining JScript (JavaScript / ECMAScript) in detail, so you might want to look at those. One of the best (I think) is “Pure JavaScript” from SAMS publishing if you can find it.

It really should be a prerequisite to know the scripting engine / language you are using to create any XMetaL customization as it will make things a lot easier.

Now, to answer your question, you can use something like the following, assuming the value you want to check is in a variable named attrval (which I redefine here):

//XMetaL Script Language JScript:
var attrval = "testing123";
var stringtofind = "123";
var pos = attrval.indexOf(stringtofind);
if(pos > -1) {
Application.Alert("attrval contains the string '" + stringtofind + "' at position " + pos);
}
else {
Application.Alert("string not found");
}

There are going to be quite a few other ways to accomplish the same thing, that one is simply the most direct. Here's another example that uses a the regular expression search method:

//XMetaL Script Language JScript:
var attrval = "testing123";
var regex = /123/;
var pos = attrval.search(regex);
if(pos > -1) {
Application.Alert("found a match for the regular expression '" + regex + "' at position " + pos);
}
else {
Application.Alert("no match");
}

Reply

Products
Downloads
Support