how to get the count of lines from multi line text string

mounika32
Tera Contributor

how to get the count of lines from multi line text string, I tried below but its returing undeined instead of 2

mounika32_0-1701927088649.png

 

1 ACCEPTED SOLUTION

User might have entered extra 'ENTER' so it is adding extra line at the end.

You can use logic like below:

var current = new GlideRecord('change_request');
current.get('fdbce3f72f323110ae17debcf699b65c');
var itar = current.variables.existing_itar_objects.toString().trim(); // use toString() to change it to string
var lineCount = itar.split('\n').length;
gs.info(lineCount);
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

15 REPLIES 15

hi @Anil Lande , I got the count but it is 3 it should be 2 right?

mounika32_0-1701930888283.png

 

@mounika32 ,

 

Its bcz there is an extra enter pressed after the 2nd entry.

 

Thanks,

Danish

 

Hello @mounika32 

 

Please use this-

var gr = new GlideRecord('sc_req_item');
gr.get('3fc5485497433110cf05f8971153af79');
var itar = gr.variables.software_requirements.toString();
var linesArray = itar.split('\n');
var lineCount = linesArray.length;
gs.info('Number of lines in the multi-line text: ' + lineCount);

 

Mark  Correct if this solves your issue and also mark  Helpful if you find my response worthy based on the impact.

User might have entered extra 'ENTER' so it is adding extra line at the end.

You can use logic like below:

var current = new GlideRecord('change_request');
current.get('fdbce3f72f323110ae17debcf699b65c');
var itar = current.variables.existing_itar_objects.toString().trim(); // use toString() to change it to string
var lineCount = itar.split('\n').length;
gs.info(lineCount);
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi @Anil Lande , Its working now when I add the trim it returns the correct count.

Thank you for your help😀