How to use StringBuilder class in PeopleCode

Unlike Java, and other object oriented languages (OOP) PeopleCode API does not deliver built in application class or a StringBuilder class for concatenating many strings together in a loop.
However, recently I found out that PeopleSoft has delivered few utility application classes in their diagnostic plug-in application packages.
The main class we are interesting here is StringBuffer Class which provides basic functionality to work with a large amount of string concatenation operations within PeopleCode.
The StringBuffer class is not as comprehensive as StringBuilder class in Java or but it is good enough to do the string operations you want.

Here is the class definition (note: the image is an indication only)

class StringBuffer
   method StringBuffer(&InitialValue As string, &MaxSize As integer);
   method Append(&New As string);
   method Reset();
   property string Value get set;
   property integer Length readonly;
   instance array of string &Pieces;
   instance integer &MaxLength;

method StringBuffer
   /+ &InitialValue as String, +/
   /+ &MaxSize as Integer +/
   &Pieces = CreateArray(&InitialValue);
   &MaxLength = &MaxSize;
   &Length = 0;

method Reset
   &Pieces = CreateArrayRept("", 0);
   &Length = 0;

method Append
   /+ &New as String +/
   Local integer &TempLength = &Length + Len(&New);
   If &Length > &MaxLength Then
      throw CreateException(210, 44, "Message Not Found", String(&MaxLength));
   &Length = &TempLength;

get Value
   /+ Returns String +/
   Local string &Temp = &Pieces.Join("", "", "", &Length);
   /* collapse array now */
   &Pieces = CreateArrayRept("", 0);
   &Pieces.Push(&Temp); /* start out with this combo string */
   Return &Temp;

set Value
   /+ &NewValue as String +/
   /* Ditch our current value */

The StringBuffer class is a simple (but useful) class which has

  • two private instances - a string array (&Pieces) and an integer variable (&MaxLength);
  • two properties - a read-write string property (Value) and read-only integer property (Length)
  • 2 methods plus constructor method.

The operation inside the class is self explanatory. The Append method can be used to add strings to the end of a string represented by the current StringBuffer. The following example initializes a StringBuffer to "Hello this is my first string!" and then appends some text to the end of the object. The &MaxSize in the constructor must specify a non-negative integer value
Before you can use this class in your PeopleCode program, you must import it into your program, using an import statement. The application package PT_DIAGNOSTICS contains the StringBuffer class. The import statement should be as follows:

Lets see an example;

   Local PT_DIAGNOSTICS:StringBuffer &MyStringBuffer = create PT_DIAGNOSTICS:StringBuffer("Hello this is my first string!", 1);
   &MyStringBuffer.Append(" This is really good.");
catch Exception &c1
   WinMessage(&c1.ToString(), 0);

/* to get the value of string use &MyStringBuffer.Value */
WinMessage(&MyStringBuffer.Value, 0);

Even though StringBuffer class provides basic functionality to manipulate strings, it comes in handy if you are dealing with files in PeopleCode for example;
While &file.ReadLine(&line)


    Blogger Comment
    Facebook Comment


Post a Comment