cancel
Showing results for 
Search instead for 
Did you mean: 

String operations in Alfresco Scripts

mitpatoliya
Star Collaborator
Star Collaborator
I am struggling in manipulating the string object in alfresco javascript.
These are the two functionalities I am looking for.

1)How to use contains function?

 var nameofDoc=new String(document.properties.title);
if(!nameofDoc.contains("TEST_"))
  not working throwing exception as
TypeError: Cannot find function contains

2)How to check the string is blank?
 
Can anybody help me out?
5 REPLIES 5

billerby
Champ on-the-rise
Champ on-the-rise
Use indexOf:

var nameofDoc=new String(document.properties.title);
if ((nameofDoc.indexOf("TEST_") != -1)){
    // TRUE
}else{
    //FALSE
}

For the empty string check:

var emptyString = "";

if (emptyString == ""){
    //TRUE
}else{
   //FALSE
}

/Erik

mitpatoliya
Star Collaborator
Star Collaborator
Thanks Erik It worked Great  😎

smcardle
Champ in-the-making
Champ in-the-making
Incase anybody else is looking to see why Script strings do not seem to act as expected with Javascript string methods.

This is because most Alfresco API calls returning String objects actuall return a java.lang.String object and NOT a javaScript string.

So, you can generally turn these into JavaScript strings and use normal javascript String methods using something like :

// If document mime type is text create a new javascript String from the java String
var jSstring = new String(document.content);

if(!jSstring.contains("TEST_") {…}

I believe you made a mistake.

Javascript String object doesn't contain "contains" method, but there is method  "contains" for java String object.

If you want to use this method then you will need to do the following operations:


var c = new java.lang.String("some text");

if(c.contains('s')) {

} else {

}


Tested in Alfresco 4.2.e

stegbth
Champ in-the-making
Champ in-the-making
Hi,

i am trying your statement, but it does not work with CE 4.2.c on Ubuntu 12.04LTS

i want to execute the script only, if the filename does NOT start with resized.


var jstrOrigName = new java.lang.String(document.properties.name);
if (!jstrOrigName.contains("resized_")) {


catalina.out
Caused by: org.mozilla.javascript.EcmaError: ReferenceError: "java" is not defined



var jSstrOrigName = new String(document.properties.name);
if (!jSstrOrigName.contains("resized_")){


catalina.out
no function resize



if (strOrigName.indexOf("resized_") != -1){


no error, no resize

could someone point me to my error?

greetz
Thomas