Service Manager / Service Center Practitioners Forum
Showing results for 
Search instead for 
Did you mean: 

converting an array to a string...

Go to solution
Cliff Cottam
Esteemed Contributor

converting an array to a string...

Is there an easy way to convert an array to a string? I am wanting to convert and array like {"This", "is", "an", "array"} to "This is an array". This would be similar to the str() or val() or recordtostring() functions but would work on an array.

I am sure it can be done with a a loop and a few strrep() statements but if there is a simplier way...
Ben Vargas
Honored Contributor

Re: converting an array to a string...

I'm not immediately aware of any single function to accomplish that so either looping or a couple functions together might be your best option, especially since you want to insert spaces between some array elements.

Maybe do three steps rather than a loop:
1. Convert array to string -- str()
2. Clip off the {" and "} -- strclpl() and strclpr()
3. Replace all ", " with " " -- strrep()
Sr. ServiceCenter & Service Manager Consultant
Honored Contributor

Re: converting an array to a string...

Hi Cliff,

The only other thing I could think of is...

$G.thearray={"This", "is", "an", "array"};$G.thestr="";for $i = 1 to lng(denull($G.thearray)) do ($G.thestr=$G.thestr+" "+$i in $G.thearray)

hope this helps...
Sam Ryburn
Occasional Contributor

Re: converting an array to a string...

I don't know of a simplier way in SC system language, but if you can use javascript you may be able to use the .join method on the array. The syntax is:
outputStr = inputArray.join( delimiter );

If you wrote a little javascript function you could use it in SC expressions.
function ats( scArray, scDelim )
var newstuff = scArray.join( scDelim );
return newstuff;

Call the js function in an expression like this:
$L.newstr=jscall("test.ats",$L.array," ")

"test" in "test.ats" would be the name of the ScriptLibrary record.

Good luck!
Kevin Seymour
Honored Contributor

Re: converting an array to a string...

There is a rad function not generally documented called strraw

Can be used like this......

$L.message={"line 1", "line 2", "line 3", "etc"}


If you want carriage returns in your string you can use this.......

$L.string.message=strraw($L.message, "newline")
Cliff Cottam
Esteemed Contributor

Re: converting an array to a string...

All excellent suggestions. Thanks to each of you. Ben and Mark did things about as I would have. Sam gets a few extra point for pusing Java - something I should learn. Keven get 10 points for to solution I thought was avaialble but could not find and the quickest, simplest solution available.
//Add this to "OnDomLoad" event