A few KPL text tools

String manipulation is one of the most common things to do in a programming language, so it is to no surprise that KPL offers many tools for doing what you want. I will try to show you and explain some of it most useful tools.

String Comparison

The most common task that I can think of is to test if two strings are the same. In other languages you would use the “==” comparison operation. in KPL you need to use the built in

// this is a case-insensitive comparison
function SimilarStr(~<STR1>~,~<STR2>~);


Edit As noted in the comments by Tom Apeland (The KPL Author!). You can use ==:

// This is a case sensitive comparison
if ( ~~ == ~~ )

remember that when using a variable to always use <> like so

if ( ~<SomeVar>~ == ~The text to compare to~ )

This will return True or False and can be used directly into any conditional structure.

String Length

Many things we might want to do to a string require us to know their number of characters. for that we use

myLength = strlen(~Some String~);

String Search

 This one is kind of interesting, it doesn’t really have its own built in method but rather uses the argument to tell it what to do. here is an example:

lets say we have a String myString with values “this is a test or is it?” if we want to find if there is an or and where it starts we use  the following

index = String(IndexOf, ~or~, ~<myString>~);

this will return the index where ~or~ starts if found. or -1 if not found.

Getting Part of a String

In many situations you might just want part of a string, a sub string per say. This is quite simple, lets say we have this string myString = ~DSC_0001.jpg~ and we want to delete the DSC_ part for whatever reason. we can do that with the following :

substr( ~<myString>~,3,8,Result); 

Now there are a few things going on here. as you can see we don’t get the sub string back as a return value, we have to first pass an object name to the method “Result”. In return, KPL will create a new String with our sub string value and it will call it Result.

Finally, the first parameter used is our main string, the second is the zero based start index and the third is the length of our sub string.

Replacing elements in a string

This is very similar to string search in its syntax. It doesn’t have its own method and uses its parameters to tell it what to do. here is an example:

Result = String( Replace, ~<myString>~,~SearchThis~,~ReplaceWithThis~);

This is very straight forward. we use the same String Method, we tell it what we want to do in the first parameter and then we provide our values.

Other Nice Tools

Getting a file name from a give path

In other languages this is usually a tedious Job using regular expressions or string searches, in KPL it is actually very simple to get a file name from a path, here is how:

myPath =~\\somepath\somefile.jpg~;

to obtain the file name we do

myFile = ~<$Str.$myPath.FileName>~;

and even simpler we can get the file name with no extension

myFile = ~<$Str.$myPath.FileName.WithoutExt>~;

Injecting a return character

Unfortunately this is not as simple as ~\n\r~. If you need your text to have a character return you need to use the following:


You can insert this anywhere in your strings.

Over ride node color and style

this is a neat feature. You can actually over ride a node set color on your entire string or in segments of that string. you do this in-line, and it sort of resembles BB code. Here is an example:

myString = ~[color 255 0 0]This is red[/color] [color 0 255 0] this is blue [/color] This uses the default node color~;

You start with the “tag” color, then you provide your RGB values separated by a space.

Similarly you can encapsulate segments with these other tags:





  1. re: String Comparison

    In addition to SimilarStr() , == also works with strings as in

    if ( ~~ == ~~ )

    SimilarStr() does a case-insensitive comparison. == is case sensitive


    1. Thanks, will update the post


Leave a Reply

Your email address will not be published. Required fields are marked *