C3 AI Documentation Home

Str

The Str Type offers a suite of functions that are essential for string manipulation and processing. In the realm of web development and data processing, efficient and accurate string handling is crucial. The Str Type provides methods to convert and manipulate string formats.

These examples demonstrate the versatility and utility of the Str Type functions. They provide an essential toolkit for string manipulation, crucial in many web development and data processing tasks. Each function addresses a specific need in string handling, from formatting and converting strings to comparing and concatenating them, illustrating the wide range of applications these methods can have in real-world scenarios.

Check if Str Type is defined

This line checks if the Str Type is defined and not null. It's a preliminary validation ensuring that the Type is loaded and available for use.

JavaScript
Str != null; // Returns true if the Type is loaded

Camel case conversions

This function converts a camel case string to a space-separated format. In the example, the input camelCaseSpace is transformed into camel case space. This is particularly useful in scenarios where you need to display camel case identifiers in a more human-readable format, like in UI elements.

JavaScript
// Camel Case to Space Separated
let f = Str.camelCaseToSpaceSeparated("camelCaseSpace");

Similar to the previous function, this one converts camel case to a dash-separated format. The input camelCase-to-dash becomes camel-case-to-dash, which is often used in URL slugs and CSS class names.

JavaScript
// Camel Case to Dash Separated
let f = Str.camelCaseToDashSeparated("camelCase-to-dash");

Capitalize first letter

This function capitalizes the first letter of the string hello world, transforming it into Hello world. It's particularly useful for formatting user input for names or titles in forms.

JavaScript
Str.capitalize('hello world');

This enhances user experience by ensuring proper capitalization in user interfaces, making the text more readable and professionally formatted.

Check if string is empty

This method checks if the given string is empty, returning true for an empty string. It's essential for validating user input in forms or processing textual data.

JavaScript
Str.isEmpty('')

Prevents processing of empty inputs in applications, enhancing efficiency and user experience by avoiding unnecessary processing of void data.

Remove trailing whitespace

Removes all trailing whitespaces from the string Hello World , resulting in Hello World. This is essential for cleaning user input data.

JavaScript
Str.trimRight('Hello World ');

Enhances data quality and reliability, especially important in data entry and processing applications where accuracy is paramount.

Convert to upper case

Converts all characters in hello world to uppercase, resulting in HELLO WORLD. This is useful for standardizing text data, like user input or database entries.

JavaScript
Str.upperCase('hello world');

Ensures consistency in data storage and retrieval, crucial for case-insensitive data handling, thereby aiding in maintaining data integrity across various applications.

Camel case to character separated

Converts a camel case string, bodyMydivContainer, to a character-separated format using ., resulting in body.mydiv.container. This is useful for converting JavaScript variable names to CSS class names or database fields.

JavaScript
Str.camelCaseToCharSeparated('bodyMydivContainer', '.');

Character separated to camel case

Converts a character-separated string to camel case format. For example, big-bad-wolf becomes bigBadWolf when using - as the separator. This function is handy for converting database field names or URL parameters to JavaScript variable names.

JavaScript
Str.charSeparatedToCamelCase("big-bad-wolf", '-');

Common prefix

Finds the common prefix among multiple strings. In this case, the common prefix of boo, boo hoo, and boop is boo. Useful for comparing strings to determine a shared starting sequence, such as in autocomplete functionalities.

JavaScript
Str.commonPrefix('boo', 'boo hoo', 'boop');

Common suffix

Identifies the common suffix among multiple strings. Here, the common suffix for boo.

JavaScript
Str.commonSuffix('boo', 'boo hoo', 'foo');

boo hoo, and foo is oo. This is useful in linguistic processing or when working with file extensions.

Concatenate strings

Concatenates multiple strings into one. The function combines str1, str2, and str3 into str1str2str3. This method is essential for constructing strings dynamically, especially in building URLs or creating long messages.

JavaScript
Str.concat('str1', 'str2', 'str3');

Compare string lengths

Compares the length of two strings. In this example, abc and def have the same length, resulting in 0 (equality). This function is valuable for sorting or validating strings based on their length.

JavaScript
Str.compareLength('abc', 'def', false);

Compare length with null handling

These examples demonstrate how the function handles null values when comparing string lengths. The function can be configured to treat null as either shorter or longer than any non-null string. This is particularly useful in applications where sorting or comparing strings with possible null values is necessary.

JavaScript
Str.compareLength(null, 'a', false); // -1
Str.compareLength('a', null, true); // -1
Was this page helpful?