Friday, September 9, 2011

String Manipulation in PeopleCode

Here are a few examples of string manipulation in PeopleCode. I will try to update this post with more examples if I think of more situations.


Number to String
This will convert a number variable to a string. This may be helpful with specific formatting or string concatenation.  For more details check out this entry in PeopleBooks.
Local number &Num = 2;
Local string &NumAsString = NumberToString("%03",&Num);
/* &NumAsString will now equal "002" */

Substring
This will take a substring of a larger string.
Local string &foo = "This is a foo string.";
Local string &start_key = "Th";
Local string &end_key = ".";

Local integer &start_pos = Find(&start_key, &foo) + Len(&start_key);
Local integer &end_pos = Find(&end_key, &foo);

Local string &result = Substring(&foo, &start_pos, &end_pos - &start_pos);
/* &result will now equal "is is a foo string" */

4 comments:

  1. Kyle,

    Thank you.

    I found this page to be VERY helpful and get me past the substring issue I was having.

    How nice of you to share your knowledge.

    yvonne

    ReplyDelete
  2. Another very useful function is DateTimeToLocalizedString().
    This can come in really handy when generating timestamps.

    This can be used liked shown below:
    DateTimeToLocalizedString(%Datetime, "ddMMyy-HHmmss");

    ReplyDelete
  3. Thanks Kyle , Tip was helpfull for me.

    Thanks
    Prashant

    ReplyDelete
  4. Very helpful, Kyle. Thanks for posting :)

    ReplyDelete