String formulas
In Ruby, strings refer to sequences of text and characters.
AppConnect supports a variety of string formulas. Formulas in AppConnect are whitelisted Ruby methods, and therefore not all Ruby methods are supported. You can always reach out to us to add additional formulas to the whitelist. Syntax and functionality for these formulas are generally unchanged. Take note that most formulas will return an error and stop the job if it tries to operate on nulls (expressed as nil in Ruby), except for present?, presence and blank?.
In the examples below, we will look at some of the methods that can be used to manipulate a string of text, which in this case the input string is 'Jean Marie'.
Conditionals
This section will cover formulas which allow you to apply conditions (if-else) to your strings.
blank?
This formula checks the input string and returns true if it is an empty string or if it is null.
Syntax
Input.blank?
Input - An input datapill. It can be a string, number, date, or datetime datatype.
Sample usage
Formula | Result |
---|---|
"Any Value".blank? | false |
123.blank? | false |
0.blank? | false |
"".blank? | true |
How it works
If the input is null or an empty string, the formula will return false. For any other data, it returns true.
is_not_true?
Converts a value to boolean and returns true if the value is not truthy.
Syntax
Input.is_not_true?
Input - An input number or a string.
Sample usage
Formula | Result |
---|---|
123.is_not_true? | false |
"false".is_not_true? | false |
0.is_not_true? | true |
"".is_not_true? | false |
nil.is_not_true? | true |
How it works
Converts the input into a boolean and returns true if the value is not truthy.
truthy vs falsey
false, nil, 0 and empty strings ("") are falsey, all other numbers and string values are truthy.
is_true?
Converts a value to boolean and returns true if the value is truthy.
Syntax
Input.is_true?
Input - An input number or a string.
Sample usage
Formula | Result |
---|---|
123.is_true? | true |
"false".is_true? | true |
0.is_true? | false |
"".is_true? | false |
nil.is_true? | false |
How it works
Converts the input into a boolean and returns true if the value is truthy.
truthy vs falsey
false, nil, 0 and empty strings ("") are falsey, all other numbers and string values are truthy.
present?
This formula will check the input and if there is a value present, it will return true. If the input is nil, an empty string or an empty list, the function will return false.
Syntax
Input.present?
Input - An input datapill. It can be a string, number, date, or list datatype.
Sample usage
Formula | Result |
---|---|
"Any Value".present? | true |
123.present? | true |
0.present? | true |
"2017-04-02T12:30:00.000000-07:00".present? | true |
nil.present? | false |
"".present? | false |
[].present? | false |
How it works
If the input is null, an empty string or an empty list, the formula will return false. For any other data, it returns true.
Evaluating a list with nil values
Only an empty list will return false.
[].present? returns false.
A list with nil and empty string will return true.
[nil,""].present? returns true.
presence
Returns the data if it exists, returns nil if it does not.
Syntax
Input.presence
Input - An input datapill. It can be a string, number, date, or datetime datatype.
Sample usage
Formula | Result |
---|---|
nil.presence | nil |
"".presence | nil |
"Any Value".presence | "Any Value" |
45.0.presence | 45.0 |
0.presence | 0 |
How it works
If the input is null or an empty string, the formula will return nil. For any other data, it returns the orignal input data.
include?
Checks if the string contains a specific substring. Returns true if it does.
Syntax
Input.include?(substring)
Input - A string input.
substring - The substring to check for.
Sample usage
Formula | Result |
---|---|
"Partner account".include?("Partner") | true |
"Partner account".include?("partner") | false |
How it works
This formula check is the string contains a specific substring. Returns true if it does, otherwise, returns false. This substring is case sensitive.
This function acts in an opposite manner from exclude?. It will return true only if the input string contains the stated keyword.
exclude?
Checks if the string contains a specific substring. Returns false if it does.
Syntax
Input.exclude?(substring)
Input - A string input.
substring - The substring to check for.
Sample usage
Formula | Result |
---|---|
"Partner account".exclude?("Partner") | false |
"Partner account".exclude?("partner") | true |
How it works
This formula check is the string contains a specific substring. Returns false if it does, otherwise, returns true. This substring is case sensitive.
This function acts in an opposite manner from include?. It will return true only if the input string does NOT contain the stated keyword.
match?
Checks if the string contains a specific pattern. Returns true if it does.
Syntax
Input.match?(pattern)
Input - A string input.
pattern - The pattern to check for.
Sample usage
Formula | Result |
---|---|
"Jean Marie".match?(/Marie/) | true |
"Jean Marie".match?(/ /) | true |
"Partner account".match?(/partner/) | false |
How it works
This formula check is the string contains a specific pattern. Returns true if it does, otherwise, returns false.
ends_with?
Checks if the string ends with a specific substring. Returns true if it does.
Syntax
Input.ends_with?(substring)
Input - A string input.
substring - The substring to check for.
Sample usage
Formula | Result |
---|---|
"Jean Marie".ends_with?("rie") | true |
"Jean Marie".ends_with?("RIE") | false |
"Jean Marie".upcase.ends_with?("RIE") | true |
How it works
This formula check is the string ends with a specific substring. Returns true if it does, otherwise, returns false.
starts_with?
Checks if the string starts with a specific substring. Returns true if it does.
Syntax
Input.starts_with?(substring)
Input - A string input.
substring - The substring to check for.
Sample usage
Formula | Result |
---|---|
"Jean Marie".starts_with?("Jean") | true |
"Jean Marie".starts_with?("JEAN") | false |
"Jean Marie".upcase.starts_with?("JEAN") | true |
How it works
This formula check is the string starts with a specific substring. Returns true if it does, otherwise, returns false.
Text manipulation
parameterize
Replaces special characters in a string. Used when app does not accept non-standard characters.
Syntax
Input.parameterize
Input - An input string.
Sample usage
Formula | Result |
---|---|
"öüâ".parameterize | "oua" |
How it works
This formula searches for special characters in the string and replaces them with standard characters. Used when app does not accept non-standard characters.
lstrip
This formula removes the white space at the beginning of the input string.
Syntax
String.lstrip
String - An input string.
Sample usage
Formula | Result |
---|---|
" Test "..lstrip | "Test " |
How it works
This formula removes white spaces from the beginning of a string. If the string doesn't have any white spaces before, the input string will be returned as is.
Quicktip: Selectively remove white spaces
To only remove white spaces from the right side, use rstrip.
To remove white spaces from the middle of the string, use gsub.
"a b c d e".gsub(" " , "") returns "abcde".
rstrip
This formula removes the white space at the end of the input string.
Syntax
String.rstrip
String - An input string.
Sample usage
Formula | Result |
---|---|
" Test "..rstrip | " Test" |
How it works
This formula removes white spaces from the end of a string. If the string doesn't have any white spaces at the end, the input string will be returned as is.
Quicktip: Selectively remove white spaces
To only remove white spaces from the left side, use lstrip.
To remove white spaces from the middle of the string, use gsub.
"a b c d e".gsub(" " , "") returns "abcde".
strip
This formula removes the white space at the beginning and the end of the input string.
Syntax
String.strip
String - An input string.
Sample usage
Formula | Result |
---|---|
"Welcome to the future of automation! ".strip | "Welcome to the future of automation!" |
" This is an example ".strip | "This is an example" |
How it works
This formula removes white spaces from both sides of a string. If the string doesn't have any white spaces before or after, the input string will be returned as is.
Quicktip: Selectively remove white spaces
To only remove white spaces from one side, use lstrip or rstrip.
To remove white spaces from the middle of the string, use gsub.
"a b c d e".gsub(" " , "") returns "abcde".
strip_tags
This formula removes html tags embedded in a string.
Syntax
String.strip_tags
String - An input string.
Sample usage
Formula | Result |
---|---|
"<p>Jean Marie</p>"..strip_tags | "Jean Marie" |
How it works
This formula check for html tags within the input string. It removes any html tags found and returns the string.
ljust
Aligns the string to left and pads with whitespace or pattern until string is specified length.
Syntax
String.ljust(length,character)
String - An input string.
length - The length of the output string.
character - (optional) The character to pad the string. If unspecified, the default pad character will be a blank space.
Sample usage
Formula | Result |
---|---|
"test"..ljust(5) | "test " |
"test"..ljust(10, "*") | "test******" |
rjust
Aligns the string to left and pads with whitespace or pattern until string is specified length.
Syntax
String.rjust(length,character)
String - An input string.
length - The length of the output string.
character - (optional) The character to pad the string. If unspecified, the default pad character will be a blank space.
Sample usage
Formula | Result |
---|---|
"test"..rjust(5) | " test" |
"test"..rjust(10, "*") | "******test" |
reverse
Inverts a string, reordering the characters in a backward manner. Case is preserved.
Syntax
String.reverse
String - An input string.
Sample usage
Formula | Result |
---|---|
"Jean Marie".reverse | "eiraM naeJ" |
" jean marie ".reverse | " eiram naej " |
gsub
Replace parts of a text string. Returns a new string with the replaced characters.
Syntax
String.gsub(find,replace)
String - An input string. You can use a datapill or a static string value.
find - The string to look for. You can use a /pattern/ syntax.
replace - The replacement string. You can define the replacement using a string or hash.
Sample usage
Formula | Result |
---|---|
"I have a blue house and a blue car".gsub("blue", "red") | "I have a red house and a red car" |
"Jean Marie".gsub("J", "M") | "Mean Marie" |
"Jean Marie".downcase.gsub("j", "M") | "Mean marie" |
Advance sample usage
Formula | Result |
---|---|
"Awesome".gsub(/[Ae]/, 'A'=>'E', 'e'=>'a') | "Ewasoma" |
"Anna's Cafe".gsub("'", "\\'") | "Annas Cafes Cafe" |
"Anna's Cafe".gsub("'", {"'"=>"\\'"}) | "Anna\\'s Cafe" |
How it works
This formula works like find and replace. It takes two input parameters:
The first input is the string that you want to replace. This is case-sensitive - so make sure to type correctly in either uppercase or lowercase to find all occurrences that are an exact match.
The second input is the new string that will be used for replacing all occurrences of first input.
Use regex pattern matching.
sub
Replaces the first occurrence of the first input value, with the second input value, within the string. This formula is case-sensitive - make sure to type in uppercase or lowercase before comparison if you are concerned about case sensitivity.
Syntax
String.sub(find,replace)
String - An input string. You can use a datapill or a static string value.
find - The string to look for. You can use a /pattern/ syntax.
replace - The replacement string. You can define the replacement using a string or hash.
Sample usage
Formula | Result |
---|---|
"Mean Marie".sub(/M/, "J") | "Jean Marie" |
"Hello".sub(/[aeiou]/, "*") | "H*llo" |
length
Returns the number of characters within an input string, including the white-spaces.\
Syntax
String.length
String - An input string.
Sample usage
Formula | Result |
---|---|
"Jean Marie".length | 10 |
" jean marie ".length | 12 |
slice
Returns a partial segment of a string.
Syntax
String.slice(start,end)
String - An input string.
start - The index of the string to start returning.
end - (optional) The number of characters to return. If unspecified, the formula will return only one character.
Sample usage
Formula | Result |
---|---|
"Jean Marie".slice(0,3) | "Jea" |
"Jean Marie".slice(5) | "M" |
"Jean Marie".slice(3,3) | "n M" |
"Jean Marie".slice(-5,5) | "Marie" |
How it works
The formula returns a partial segment of a string. It takes in 2 parameters - the first parameter is the index that decides which part of the string to start returning from (first letter being 0 and subsequently progressing incrementally, negative numbers will be taken from the last character), the second parameter decides how many characters to return. If only the first parameter is passed in, only 1 character will be returned.
scan
Scan the string for the pattern to retrieve and return an array
Syntax
String.scan(pattern)
String - An input string.
pattern - The pattern to search for.
Sample usage
Formula | Result |
---|---|
"Thu, 01/23/2014".scan(/\d+/) | ["01","23","2014"] |
"Thu, 01/23/2014".scan(/\d+/).join("-") | "01-23-2014" |
encode
Returns the string in a given encoding.
Syntax
String.encode(encoding)
String - An input string.
encoding - Name of the encoding (e.g. Windows-1252).
Sample usage
Formula |
---|
"Jean Marie".encode("Windows-1252") |
transliterate
Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character which defaults to '?'.
Syntax
String.transliterate
String - An input string.
Sample usage
Formula | Result |
---|---|
"Chloé".transliterate | "Chloe" |
: Replaces special characters in a string.
Text case manipulation
This section covers formulas which allow you to change the case of certain parts of a word.
capitalize
Converts the input string into sentence case, i.e. the first character of each sentence is capitalized.
Syntax
String.capitalize
String - An input string.
Sample usage
Formula | Result |
---|---|
"ticket opened. Gold SLA".capitalize | "Ticket opened. gold sla" |
"jean MARIE".capitalize | "Jean marie" |
titleize
Converts the input string into title case, i.e. the first character of each word is capitalized.
Syntax
String.titleize
String - An input string.
Sample usage
Formula | Result |
---|---|
"ticket opened. Gold SLA".titleize | "Ticket Opened. Gold Sla" |
"jean MARIE".titleize | "Jean Marie" |
upcase
Convert text to uppercase.
Syntax
String.upcase
String - An input string.
Sample usage
Formula | Result |
---|---|
"Automation at it's FINEST!".upcase | "AUTOMATION AT IT'S FINEST!" |
"Convert to UPCASE".upcase | "CONVERT TO UPCASE" |
How it works
This formula searches for any lowercase character and replace it with the uppercase characters.
Quicktip: Search strings better with upcase
Search formulas like (gsub or sub) uses case sensitive characters. Use the upcase formula ensure that all characters are in the same case.
downcase
Convert text to lowercase.
Syntax
String.downcase
String - An input string.
Sample usage
Formula | Result |
---|---|
"Automation at it's FINEST!".downcase | "automation at it's finest! |
"Convert to DOWNCASE".downcase | "convert to downcase" |
How it works
This formula searches for any uppercase character and replace it with the lowercase characters.
Quicktip: Search strings better with downcase
Search formulas like (gsub or sub) uses case sensitive characters. Use the downcase formula ensure that all characters are in the same case.
quote
Quotes a string, escaping any ' (single quote) characters
Syntax
String.quote
String - An input string.
Sample usage
Formula | Result |
---|---|
"Paula's Baked Goods".quote | "Paula''s Baked Goods" |
Converting to arrays and back
This section shows how you can manipulate strings into arrays.
split
This formula divides a string around a specified character and returns an array of strings.
Syntax
String.split(char)
String - An input string value. You can use a datapill or a static value.
char - (optional) The character at which to split the text. This is case sensitive. If no character is defined, then by default, strings are split by white spaces.
Sample usage
Formula | Result |
---|---|
"Ms-Jean-Marie".split("-") | ["Ms", "Jean", "Marie"] |
"Ms Jean Marie".split | ["Ms", "Jean", "Marie"] |
"Split string".split() | ["Split", "string"] |
"Split string".split("t") | ["Spli", " s", "ring"] |
"01/23/2014".split("/") | ["01", "23", "2014"] |
"01/23/2014".split("/").join("-") | "01-23-2014" |
How it works
This formula looks for the specified character in the input string. Every time it is found, the input will be split into a new string.
Split character(s)
You can use a string of characters together as the split argument (e.g. "and").
"You and Me".split("and") returns ["You","Me"].
bytes
Returns an array of bytes for a given string.
Syntax
String.bytes
String - An input string.
Sample usage
Formula | Result |
---|---|
"Hello".bytes | ["72","101","108","108","111"] |
Conversion formulas
To convert a value of other data types, e.g. number, date, into a string, use the to_s formula.
Conversion of other data types to strings
to_s
Converts data to a string (text) datatype.
Syntax
Input.to_s
Input - Any input data. You can use number, array, object, or datetime datatypes.
Sample usage
Formula | Result |
---|---|
-45.67.to_s | "-45.67" |
"123".to_s | "123" |
[1,2,3].to_s | "[1,2,3]" |
{key: "AppConnect"}.to_s | "{:key=>"AppConnect"}"" |
"2020-06-05T17:13:27.000000-07:00".to_s | "2020-06-05T17:13:27.000000-07:00" |
"2020-06-05T17:13:27.000000-07:00".to_s(:short) | "05 Jun 17:13" |
"2020-06-05T17:13:27.000000-07:00".to_s(:long) | "June 05, 2020 17:13" |
How it works
This formula returns a string representation of the input data.
Quicktip: Output is a string datatype.
A string representation of a list cannot be used in a repeat step.
ordinalize
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
Syntax
Input.ordinalize
Input - Any input number.
Sample usage
Formula | Result |
---|---|
1.ordinalize | "1st" |
2.ordinalize | "2nd" |
3.ordinalize | "3rd" |
1003.ordinalize | "1003rd" |
-3.ordinalize | "-3rd" |
Conversion of strings to other data types
to_f
Converts data to a float (number) datatype.
Syntax
Input.to_f
Input - An number input data. You can use a string datatype or a integer datatype.
Sample usage
Formula | Result |
---|---|
45.to_f | 45.0 |
-45.to_f | -45.0 |
"45.67".to_f | 45.67 |
"AppConnect".to_f | 0 |
How it works
This formula checks whether the input contains any numbers, if no numbers are found, it returns 0. If the number does not have a decimal point, .0 will be added the number.
to_i
Converts data to an integer (whole number) datatype.
Syntax
Input.to_i
Input - An number input data. You can use a string datatype or a float datatype.
Sample usage
Formula | Result |
---|---|
45.43.to_i | 45 |
-45.43.to_i | -45 |
"123".to_i | 123 |
"AppConnect".to_i | 0 |
How it works
This formula checks whether the input contains any numbers, if no numbers are found, it returns 0. If the number has a decimal point, everything after the decimal will be omitted.
Check for integers
You can use this formula to check if a string contains an integer. If the input does not contain any numbers, the formula will return 0.
to_country_alpha2
Convert alpha-3 country code or country name to alpha2 country code (first 2 initials).
Syntax
Input.to_country_alpha2
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_country_alpha2 | "GB" |
"United Kingdom".to_country_alpha2 | "GB" |
to_country_alpha3
Convert alpha-2 country code or country name to alpha3 country code (first 3 initials).
Syntax
Input.to_country_alpha3
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_country_alpha3 | "GBR" |
"United Kingdom".to_country_alpha3 | "GBR" |
to_country_name
Convert alpha-2/3 country code or country name to ISO3166 country name.
Syntax
Input.to_country_name
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_country_name | "United Kingdom" |
"GB".to_country_name | "United Kingdom" |
to_currency
Formats integers/numbers to a currency-style.
Syntax
Input.to_currency
Input - Any input string.
Sample usage
Formula | Description | Result |
---|---|---|
"345.60".to_currency | Adds default currency symbol "$" | "$345.60" |
Advance sample usage
Learn more about advance usage for the to_currency formula. A comma-separated combination of these may be used to achieve the desired currency format. For example:
1"12345.678".to_currency(delimiter: ".", format: "%n %u", precision: 2, separator: ",", unit: "€")2
Formula | Description | Result |
---|---|---|
"345.60".to_currency(unit: "€") | Changes the default currency unit | "€345.60" |
"345.60".to_currency(format: "%n %u") | Changes the position of the number relative to the unit (where the number is represented by %n and the currency unit is represented by %u). Accepts 0 or 1 spaces in between. Defaults to "%u%n". | "345.60 $" |
"-345.60".to_currency(negative_format: "(%u%n)") | Specifies the format when the number is negative (where the number is represented by %n and the currency unit is represented by %u). | "($345.60)" |
"345.678".to_currency | Precision defaults to 2 decimal places | "$345.68" |
"345.678".to_currency(precision: 3) | Change the precision by specifying the number of decimal places | "$345.678" |
"345.678".to_currency(separator: ",") | Specify the decimal separator as ".", "," or " ". Defaults to ".". | "$345,68" |
"12345.678".to_currency(delimiter: ".") | Specify the thousands separator as ",", "." or " ". Defaults to ",". | ""$12.345.68" |
to_currency_code
Convert alpha-2/3 country code or country name to ISO4217 currency code
Syntax
Input.to_currency_code
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_currency_code | "GBP" |
"US".to_currency_code | "USD" |
to_currency_name
Convert alpha-3 currency code or alpha-2/3 country code or country name to ISO4217 currency name.
Syntax
Input.to_currency_name
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_currency_code | "Pound" |
"USD".to_currency_code | "Dollars" |
to_currency_symbol
Convert alpha-3 currency code or alpha-2/3 country code or country name to ISO4217 currency symbol.
Syntax
Input.to_currency_symbol
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"GBR".to_currency_symbol | "£" |
"USD".to_currency_symbol | "$" |
to_phone
Converts string or number to a formatted phone number (user-defined).
Syntax
Input.to_phone
Input - Any input string or number.
Sample usage
Formula | Result |
---|---|
"5551234".to_phone | 555-1234 |
1235551234.to_phone | 123-555-1234 |
1235551234.to_phone(area_code: true) | (123) 555-1234 |
1235551234.to_phone(delimiter: " ") | 123 555 1234 |
1235551234.to_phone(area_code: true, extension: 555) | (123) 555-1234 x 555 |
1235551234.to_phone(country_code: 1) | +1-123-555-1234 |
"123a456".to_phone | 123a456 |
to_state_code
Convert state name to code.
Syntax
Input.to_state_code
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"California".to_state_code | CA |
to_state_name
Convert state code to name.
Syntax
Input.to_state_name
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"CA".to_state_name | CALIFORNIA |
bytesize
Returns the length of a given string in bytes.
Syntax
Input.bytesize
Input - Any input string.
Sample usage
Formula | Result |
---|---|
"Hello".bytesize | 5 |