User login

  

String Functions

The following lists alphabetically built-in PTL functions available for manipulating and working with strings.

IsDigit StringInsert StringSplit
StringAppend StringRemove StringStartsWith
StringBuild StringRemoveFirst StringSubst
StringEntries StringRemoveLast StringTrimLeft
StringFind StringReplace StringTrimRight

 

 

These functions are placed in Strings package.

 

IsDigit

Indicates whether the specified character is categorized as a decimal digit.

Syntax

bool  IsDigit( char element)

Parameters

  • element - Any char.

 

StringAppend

Appends the specified string to the end of the string, and returns the resulting string.

Syntax

string StringAppend( string text, string stringToAppend)

Parameters

  • text - Any string.
  • stringToAppend - String to append.

Example

string str;               

str = StringAppend("comp", "iler");   // str <- "compiler"

 

StringBuild

Creates a new string to the value indicated by a specified character or string repeated a specified number of times

Syntax

string StringBuild( char character, int repeat)

string StringBuild( string str, int repeat)

Parameters

  • character - Any character.
  • repeat - Number of repeat character

Example

string str;               

str = StringBuild('a', 4);   // str <- "aaaa"

 

StringEntries

Return the count of entries the specified symbol into the sprcified string

Syntax

int StringEntries(string  baseString, char symbol)  

 

StringFind

Searches for the specified substring and returns the position of its first occurrence in the string. If no specified substring is found, the NOT_FOUND constant is returned (NOT_FOUND equal to (-1)).

Syntax

int StringFind( string text, string pattern)

int StringFind( string text, char pattern)

int StringFind( string text, string pattern, int startPos)

int StringFind( string text, string pattern, int startPos, int count)

int StringFind( string text, char pattern, int startPos)

int StringFind( string text, char pattern, int startPos, int count)

Parameters

  • text - Any string.
  • pattern - Substring for which to search.
  • startPos - Positive integer specifying the position in the string from which to search.
  • count - Integer specifying the number of characters from the starting position within which to search.

Example

int res;

res = StringFind("xyzababc2354", "abc");   // res <- 5

res = StringFind("123", "abc");            // res <- NOT_FOUND

res = StringFind("abxyzababc2354", 'a', 3);       // res <- 5

res = StringFind("1234567890ab", 'a', 0, 5);      // res <- NOT_FOUND

 

StringInsert

Inserts the specified substring in the specified position of the string, and returns the resulting string.

Syntax

string StringInsert( string text, int pos, string stringToInsert)

Parameters

  • text - Any string.
  • pos - Positive integer specifying the position in the string at which to insert the substring.
  • stringToInsert - String to insert.

Example

string res;      

res = StringInsert("abc", 1, "123");       // res <- "a123bc"

 

StringRemove

Removes the substring of specified length starting from the specified position from the string, and returns the resulting string.

Syntax

string StringRemove( string text, int pos, int length)

string StringRemove( string text, int pos)

Parameters

  • text - Any string.
  • pos - Positive integer specifying the starting position of the substring to remove in the string.
  • length - Integer specifying the length of the substring to remove.

Example

string str;

str = StringRemove("abc123xyz", 3, 3);       // str <- "abcxyz"

str = StringRemove("abc123xyz", 3);       // str <- "abc"

 

StringRemoveFirst

Removes the first character (or substring of the specified length) from the beginning of the string, and returns the resulting string.

Syntax

string StringRemoveFirst( string text)

string StringRemoveFirst( string text, int length)

Parameters

  • text - Any string.
  • length - Integer specifying the length of the substring to remove.

Example

string str;

str = StringRemoveFirst("abc123xyz");       // str <- "bc123xyz"

str = StringRemoveFirst("abc123xyz", 2);    // str <- "c123xyz"

 

StringRemoveLast

Removes the last character (or substring of the specified length) from the end of the string, and returns the resulting string.

Syntax

string StringRemoveLast( string text, int length)

string StringRemoveLast( string text)

Parameters

  • text - Any string.
  • length - Integer specifying the length of the substring to remove.

Example

string str;

str = StringRemoveLast("abc123xyz");       // str <- "abc123xy"

str = StringRemoveLast("abc123xyz",2);     // str <- "abc123x"

 

StringReplace

Replaces all occurrences of the specified substring in the string with the new substring, and returns the resulting string.

Syntax

string StringReplace( string text, string old, string new)

string StringReplace( string text, char old, char new)

Parameters

  • text - Any string.
  • old - String to be replaced.
  • new - String or character with which to replace the found one.

Example

string str;

str = StringRemoveLast("abc123abc", "abc", "wxyz");       // str <- "wxyz123wxyz"

 

StringSplit

Returns a string array that contains the substrings in this instance that are delimited by specified character.

Syntax

void  StringSplit( string[] array, string baseString, char delimiter)

void  StringSplit( string[] array, string baseString,  char delimiter, bool removeEmptyEntries)  

 

StringStartsWith

Returns true if the specified string starts with the specified substring, and false otherwise.

Syntax

bool StringStartsWith( string text, string substring)

Parameters

  • text - Any string.
  • substring - Any string.

Example

bool res;

res = StringStartsWith("abc123abc", "ab");       // res <- True

res = StringStartsWith("abc123abc", "12");       // res <- False

 

StringSubst

Returns the substring of specified length starting from the specified position of the string.

Syntax

string StringSubst( string text, int pos, int length)

string StringSubst( string text, int pos)

Parameters

  • text - Any string.
  • pos - Positive integer specifying the starting position of the returned substring in the string.
  • length - Integer specifying the length of the returned substring.

Example

string str;

str = StringSubst("abc123abc", 5, 2);    // str <- "3a"

str = StringSubst("abc123abc", 5);       // str <- "3abc"

 

StringTrimLeft

Trims whitespace characters (spaces, tabs, line feeds, carriage returns) from the left-hand side of the string, and returns the trimmed string.

Syntax

string StringTrimLeft( string text)

Parameters

  • text - Any string.

Example

string str;

str = StringTrimLeft("  abc");       // str <- "abc"

 

StringTrimRight

Trims whitespace characters (spaces, tabs, line feeds, carriage returns) from the right-hand side of the string, and returns the trimmed string.

Syntax

string StringTrimRight( string text)

Parameters

  • text - Any string.

Example

string str;

str = StringTrimRight("abc   ");       // str <- "abc"

 

12345