I was recently asked: “What is the most performant way of concatenating strings in AX?”
Traditionally there have been two approaches to build and/or format strings in AX, strFmt and + ing, but you can also leverage the .NET libraries like System.String.Concat and System.Text.StringBuilder.
In this scenario, the goal was to build up a string of a fixed set of elements:
strFmt
str ss = strFmt('Context=%1, Activity=%2, ActivityId=%3, Type=Stop, Tag1=%6, Tag2=%7, Tag3=%8, Tag4=%9', context, activity, activityId, tag1, tag2, tag3, tag4);
+ ing
str ss = 'Context=' + context + ', Activity=' + activity + ', ActivityId=' + activityId + ', Type=Stop, Tag1=' + tag1 + ', Tag2=' + tag2 + ', Tag3=' + tag3 + ', Tag4=' + tag4;
System.String::Concat
System.String s1 = System.String::Concat('Context=', context, ', Activity=', activity); System.String s2 = System.String::Concat(', ActivityId=', activityId, 'Type=Stop, tag1=%', tag1); System.String s3 = System.String::Concat(', tag2=', tag2, ', tag3=', tag3); System.String s4 = System.String::Concat(s2, s3, ', tag4=', tag4); str s5 = System.String::Concat(s1, s4);
System.Text.StringBuilder
System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("Context="); sb.Append(context); sb.Append(", Activity="); sb.Append(activity); sb.Append(", ActivityId="); sb.Append(activityId); sb.Append(", Type=Stop, tag1="); sb.Append(tag1); sb.Append(", tag2="); sb.Append(tag2); sb.Append(", tag3="); sb.Append(tag3); sb.Append(", tag4="); sb.Append(tag4); str result = sb.ToString();
And the results …