How to split String and store in array

Sironi
Kilo Sage

Hi,

 

Could you please help me on below issue.

How to split CN = value only and store in array variable 

 

CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root

find_real_file.png

 

i tried below one but not working.

var gr=current.u_member.split(',');
var gg=gr.split('CN=');
gs.addInfoMessage(gs.getMessage("GR:"+gg));

 

it is returning single CN= value, but there are 2 CN values. 

some records had 10 CN values. so how can we do this time.

 

How to fix this issue.

1 ACCEPTED SOLUTION

Try below.

var getinarray=[];
var textis='CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root';
textis=textis.replace(/\^/g,','); //replaces ^ with comma
var splitcomma=textis.split(','); //splits by comma
for(var i=0;i<splitcomma.length;i++)
{
if(splitcomma[i].indexOf('CN=')>-1) //if contains CN it will go in if loop
{
getinarray.push(splitcomma[i].split('=')[1]);
}
}
gs.print('Value is '+getinarray);

View solution in original post

13 REPLIES 13

Hi Jaspal,

Thanks for reply,

it is working great, can we show only BTHORMANVM1 , COBROUTEV2 and push these two values into array?

Try below.

var getinarray=[];
var textis='CN=COBROUTEV2,OU=Servers,OU=Computers,OU=COB,DC=abc,DC=root^CN=BTHORMANVM1,OU=Workstations,DC=abc,DC=root';
textis=textis.replace(/\^/g,','); //replaces ^ with comma
var splitcomma=textis.split(','); //splits by comma
for(var i=0;i<splitcomma.length;i++)
{
if(splitcomma[i].indexOf('CN=')>-1) //if contains CN it will go in if loop
{
getinarray.push(splitcomma[i].split('=')[1]);
}
}
gs.print('Value is '+getinarray);

Thanks Jaspal, Thanks a lot. thanks for your answer.

when i used below script it is showing "," additionally so how to remove that.

var arr = [];
var textis = current.u_member.toString();
textis = textis.replace(/\^/g, ','); //replaces ^ with comma
var splitcomma = textis.split(','); //splits by comma
for (var i = 0; i < splitcomma.length; i++) {
if (splitcomma[i].indexOf('CN=') > -1) //if contains CN it will go in if loop
{
gs.print('Value is ' + splitcomma[i]);
gs.addInfoMessage(gs.getMessage(splitcomma[i]));
var spl = splitcomma[i].split("CN=");
gs.addInfoMessage(gs.getMessage(spl));
arr.push(spl);

}
}
for (var j = 0; j < arr.length; j++) {
gs.addInfoMessage(gs.getMessage("ARRAY VALUES " + arr[j]));
}

 

find_real_file.png