Skip to content

Computing Functions Strings

anymaker edited this page Apr 4, 2021 · 2 revisions

String Functions

CharAt Return one character from position. The first position has index 0
EndsWith Tests if string ends with the specified suffix.
ExtractDigits Retrieves all digits from a string
Format Formats a number into a string by mask
IndexOf Returns the index within string of the first occurrence of the specified substring.
IsBlank Checks if string contain only whitespace characters or null
IsEmpty Checks that the string contains no characters or null.
Like Returns true if the condition specified in the template is met, similar to the 'like' operation as in databases
LikeRegexp Return true if the condition specified by the regular expression is true
LTrim Remove leading whitespace
NormalizeSpaces Returned string with normalized spaces
PadLeft Adds characters to the left of the string until the string reaches the specified length.
PadRight Adds characters to the right of the string until the string reaches the specified length.
RemoveSpaces Remove all space characters
RemoveWhitespaces Remove all whitespaces characters
Replace Returns a string resulting from replacing all occurrences of oldString in this string with newString.
RTrim Remove trailing whitespace
StartsWith Tests if string starts with the specified prefix.
Substring Returns a string that is a substring of this string.
ToString Convert any value to String
Trim Returns a string whose value is this string, without leading and trailing whitespace characters
TrimToNull Returns a string without leading and trailing whitespace characters or null if string is empty

CharAt

Return one character from position. The first position has index 0.

Syntax

charAt(string, position)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes The incoming string from which to extract the character at the given position.
position int Yes The position at which the desired character is located.

Output

Type: java.lang.String
The string with the desired character.

Examples

Formula formula = new Formula("charAt('abcdf', 2)");
String  str = engine.calc(formula, String.class);
assertEquals("c", str);

EndsWith

Tests if string ends with the specified suffix.

Syntax

endsWith(string, suffix)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string to test
suffix String Yes suffix

Output

Type: boolean
If the string ends with a suffix, then true, otherwise false.

Examples

Formula formula = new Formula("endsWith('abcdf', 'df')");
boolean isEndsWith = engine.calc(formula, Boolean.class);
assertTrue(isEndsWith);

ExtractDigits

Retrieves all digits from a string.
The function does not extract the sign of a number.

Syntax

extractDigits(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: java.lang.String
All digits contained in the string.

Examples

Formula formula = new Formula("extractDigits('1a2b3c-45(6)d')");
String result = engine.calc(formula, String.class);
assertEquals("123456", result);

Format

Formats a number into a string by mask.
Example:
  number: 12345678901
  mask: S(XXX)XXX-XXX XX
  result: +(123)456-789 01

Syntax

format(value, mask, digitHolder, signHolder)

Incoming parameters

Parameter Name Type Require Description Default Value
value String Yes Incoming value to format
mask String Yes Mask to format
digitHolder String No Sign holder 'S'
signHolder String No Digit holder 'X'

Output

Type: java.lang.String
Formatted string.

Examples

Formula formula = new Formula("format('12345678901', 'S(XXX)XXX-XXX XX')");
String result = engine.calc(formula, String.class);
assertEquals("+(123)456-789 01", result);

IndexOf

Returns the index within string of the first occurrence of the specified substring.

Syntax

indexOf(string, substring)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string
substring String Yes A substring to search for in an incoming string

Output

Type: int
Position in the incoming string, or 0 if the substring was not found.

Examples

Formula formula = new Formula("indexOf('abcdf', 'aaa')");
int pos = engine.calc(formula, Integer.class);
assertEquals(-1, pos);

formula = new Formula("indexOf('abcdf', 'ab')");
pos = engine.calc(formula, Integer.class);
assertEquals(0, pos);

formula = new Formula("indexOf('abcdf', 'bc')");
pos = engine.calc(formula, Integer.class);
assertEquals(1, pos);

IsBlank

Checks if string contain only whitespace characters or null.

Syntax

isBlank(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: boolean
true when string null or empty or contain white space characters.

Examples

Formula formula = new Formula("isBlank('abcdf')");
boolean isBlank = engine.calc(formula, Boolean.class);
assertFalse(isBlank);

formula = new Formula("isBlank(null)");
isBlank = engine.calc(formula, Boolean.class);
assertTrue(isBlank);

formula = new Formula("isBlank(' \t\n ')");
isBlank = engine.calc(formula, Boolean.class);
assertTrue(isBlank);

IsEmpty

Checks that the string contains no characters or null.

Syntax

isEmpty(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: boolean
true when the string contains no characters or null.

Examples

Formula formula = new Formula("isEmpty('abcdf')");
boolean isBlank = engine.calc(formula, Boolean.class);
assertFalse(isBlank);

formula = new Formula("isEmpty(' ')");
isBlank = engine.calc(formula, Boolean.class);
assertFalse(isBlank);

formula = new Formula("isEmpty('')");
isBlank = engine.calc(formula, Boolean.class);
assertTrue(isBlank);

formula = new Formula("isEmpty(null)");
isBlank = engine.calc(formula, Boolean.class);
assertTrue(isBlank);

Like

Returns true if the condition specified in the template is met, similar to the 'like' operation as in databases.

Syntax

like(string, template)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes The incoming string to test.
template String Yes expression, where
character '_' substitute any one characters
character '%' substitute more than one of any characters.

Output

Type: boolean
True if the tested string matches the search expression.

Examples

assertTrue(engine.calc(new Formula("like('abcdfab', 'a%b')"), Boolean.class));
assertTrue(engine.calc(new Formula("like('abcdfabghc', 'a%b%c')"), Boolean.class));

LikeRegexp

Return true if the condition specified by the regular expression is true

Syntax

likeRegexp(string, template)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes The incoming string to test
template String Yes regexp expression

Output

Type: boolean
True if the tested string matches the search regexp expression.

Examples

assertTrue(engine.calc(new Formula("likeRegexp('abcdfab', '^a.*b$')"), Boolean.class));
assertTrue(engine.calc(new Formula("likeRegexp('abcdfabghc', '^a.*b.*c$')"), Boolean.class));

LTrim

Remove leading whitespace

Syntax

ltrim(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without leading whitespace.

Examples

Formula formula = new Formula("lTrim('   abcdef')");
String result = engine.calc(formula, String.class);
assertEquals("abcdef", result);

formula = new Formula("lTrim('abcdef')");
result = engine.calc(formula, String.class);
assertEquals("abcdef", result);

formula = new Formula("lTrim(null)");
result = engine.calc(formula, String.class);
assertNull(result);

formula = new Formula("lTrim(' \t\n ')");
result = engine.calc(formula, String.class);
assertEquals("", result);

NormalizeSpaces

Returned string with normalized spaces

Syntax

normalizeSpaces(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String with normalized spaces.

Examples

Formula formula = new Formula("normalizeSpaces('a b   cd  f')");
String result = engine.calc(formula, String.class);
assertEquals("a b cd f", result);

formula = new Formula("normalizeSpaces(' a b c d\nf ')");
result = engine.calc(formula, String.class);
assertEquals(" a b c d\nf ", result);

PadLeft

Adds characters to the left of the string until the string reaches the specified length.

Syntax

padLeft(string, quantity, sample)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null
quantity int Yes Total length of the string after adding characters null
sample String Yes Added symbols null

Output

Type: String
String with added characters.

Examples

Formula formula = new Formula("padLeft('AAA', 10, 'B')");
String result = engine.calc(formula, String.class);
assertEquals("BBBBBBBAAA", result);

formula = new Formula("padLeft('AAA', 2, 'B')");
result = engine.calc(formula, String.class);
assertEquals("AAA", result);

PadRight

Adds characters to the right of the string until the string reaches the specified length.

Syntax

padRight(string, quantity, sample)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null
quantity int Yes Total length of the string after adding characters null
sample String Yes Added symbols null

Output

Type: String
String with added characters.

Examples

Formula formula = new Formula("padRight('AAA', 10, 'B')");
String result = engine.calc(formula, String.class);
assertEquals("AAABBBBBBB", result);

formula = new Formula("padRight('AAA', 2, 'B')");
result = engine.calc(formula, String.class);
assertEquals("AAA", result);

RemoveSpaces

Remove all space characters

Syntax

removeSpaces(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without space characters

Examples

formula = new Formula("removeSpaces(' a b c d f ')");
result = engine.calc(formula, String.class);
assertEquals("abcdf", result);

RemoveWhitespaces

Remove all whitespaces characters

Syntax

removeWhitespaces(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without whitespaces characters

Examples

formula = new Formula("removeWhitespaces(' a b\nc\td f ')");
result = engine.calc(formula, String.class);
assertEquals("abcdf", result);

Replace

Returns a string resulting from replacing all occurrences of oldString in this string with newString.

Syntax

padRight(string, oldString, newString)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null
oldString String Yes The old string null
newString String Yes The new string null

Output

Type: String
String with replaced characters.

Examples

Formula formula = new Formula("replace('abcdefbcgh', 'bc', 'BC')");
String res = engine.calc(formula, String.class);
assertEquals("aBCdefBCgh", res);

RTrim

Remove trailing whitespace

Syntax

rtrim(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without trailing whitespace.

Examples

Formula formula = new Formula("rTrim('abcdef   ')");
String result = engine.calc(formula, String.class);
assertEquals("abcdef", result);

StartsWith

Tests if string starts with the specified prefix.

Syntax

startsWith(string, prefix)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string to test
prefix String Yes Prefix

Output

Type: boolean
If the string starts with a prefix, then true, otherwise false.

Examples

Formula formula = new Formula("startsWith('abcdf', 'ab')");
boolean isEndWith = engine.calc(formula, Boolean.class);
assertTrue(isEndWith);

formula = new Formula("startsWith('abcdf', 'df')");
isEndWith = engine.calc(formula, Boolean.class);
assertFalse(isEndWith);

Substring

Returns a string that is a substring of this string.
Without any exception when the index goes out of bound.
The start and length can be less than zero. In this case, the calculation is carried out from the end.

Syntax

substring(string, start, length)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes The incoming string
start int Yes The start position
length int Yes Length of substring

Output

Type: java.lang.String
A substring from the incoming string.

Examples

Formula formula = new Formula("substring('abcdefgh', 0, 2)");
String res = engine.calc(formula, String.class);
assertEquals("ab", res);

formula = new Formula("substring('abcdefgh', 2, 5)");
res = engine.calc(formula, String.class);
assertEquals("cdefg", res);

formula = new Formula("substring('abcdefgh', 2, 50)");
res = engine.calc(formula, String.class);
assertEquals("cdefgh", res);

formula = new Formula("substring('abcdefgh', 20, 50)");
res = engine.calc(formula, String.class);
assertEquals("", res);

formula = new Formula("substring('abcdefgh', -5, 2)");
res = engine.calc(formula, String.class);
assertEquals("de", res);

formula = new Formula("substring('abcdefgh', -5, -2)");
res = engine.calc(formula, String.class);
assertEquals("bc", res);

formula = new Formula("substring('abcdefgh', -50, 2)");
res = engine.calc(formula, String.class);
assertEquals("", res);

formula = new Formula("substring('abcdefgh', -5, 20)");
res = engine.calc(formula, String.class);
assertEquals("defgh", res);

formula = new Formula("substring('abcdefgh', 5, -20)");
res = engine.calc(formula, String.class);
assertEquals("abcde", res);

ToString

Convert any value to String

Syntax

toString(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
The value as string.

Trim

Returns a string whose value is this string, without leading and trailing whitespace characters

Syntax

trim(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without leading and trailing whitespace characters.

Examples

Formula formula = new Formula("trim('   abcdef   ')");
String  result = engine.calc(formula, String.class);
assertEquals("abcdef", result);

TrimToNull

Returns a string without leading and trailing whitespace characters or null if string is empty

Syntax

trimToNull(string)

Incoming parameters

Parameter Name Type Require Description Default Value
string String Yes Incoming string null

Output

Type: String
String without leading and trailing whitespace characters or null if string is empty.

Examples

Formula formula = new Formula("trimToNull('   abcdef  ')");
String result = engine.calc(formula, String.class);
assertEquals("abcdef", result);

formula = new Formula("trimToNull(null)");
result = engine.calc(formula, String.class);
assertNull(result);

formula = new Formula("trimToNull(' \t\n ')");
result = engine.calc(formula, String.class);
assertEquals(null, result);

formula = new Formula("trimToNull('    ')");
result = engine.calc(formula, String.class);
assertEquals(null, result);

Clone this wiki locally