CString is an immutable string class in the cava.lang package of the CAVA scripting language used in OpenSearch. Once created, a CString object cannot be modified.
The simplest way to create a CString is with a string literal:
CString a = "opensearch";This is equivalent to:
byte[] value = {(byte)'o', (byte)'p', (byte)'e', (byte)'n', (byte)'s', (byte)'e', (byte)'a', (byte)'r', (byte)'c', (byte)'h'};
CString a = new CString(value, 0, 10);Constructors
| Signature | Description |
|---|---|
CString(byte[] value, int offset, int count) | Creates a string from a character array |
CString(CString original) | Creates a copy of an existing CString object |
Methods
| Signature | Description |
|---|---|
int length() | Returns the length of the string |
boolean isEmpty() | Returns true if the string length is 0 |
byte byteAt(int index) | Returns the character at the specified index |
boolean equals(CString anotherString) | Returns true if the two strings are identical |
boolean equalsIgnoreCase(CString anotherString) | Returns true if the two strings are identical, ignoring case |
int compareTo(CString anotherString) | Compares two strings lexicographically |
int compareToIgnoreCase(CString anotherString) | Compares two strings lexicographically, ignoring case |
boolean startsWith(CString prefix, int toffset) | Returns true if the string starts with the given prefix at the specified index |
boolean startsWith(CString prefix) | Returns true if the string starts with the given prefix |
boolean endsWith(CString suffix) | Returns true if the string ends with the given suffix |
int indexOf(CString str, int fromIndex) | Returns the index of the first occurrence of a substring, searching from fromIndex |
int indexOf(CString str) | Returns the index of the first occurrence of a substring |
int lastIndexOf(CString str, int fromIndex) | Returns the index of the last occurrence of a substring, searching backward from fromIndex |
int lastIndexOf(CString str) | Returns the index of the last occurrence of a substring |
CString substring(int beginIndex, int endIndex) | Returns a substring from beginIndex to endIndex |
CString substring(int beginIndex) | Returns a substring from beginIndex to the end of the string |
CString[] split(CString str) | Splits the string by a delimiter; trailing empty substrings are removed |
CString[] split(CString str, int limit) | Splits the string by a delimiter, with a limit on the result count |
CString trim() | Removes leading and trailing spaces |
void getChars(byte[] dst, int dstBegin) | Copies all characters into a destination array |
void getChars(int srcBegin, int srcEnd, byte[] dst, int dstBegin) | Copies a range of characters into a destination array |
Method details
CString(byte[] value, int offset, int count)
Creates a string from a character array.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | byte[] | The character array used to create the string. |
offset | int | The index of the first character. Must be ≥ 0; otherwise an error occurs. |
count | int | The number of characters to use. Must be ≥ 0; otherwise an error occurs. |
Example
byte[] value = {(byte)'a', (byte)'b', (byte)'c'};
CString test = new CString(value, 0, 3);
// Equivalent to: CString test = "abc";CString(CString original)
Creates a copy of an existing CString object.
Parameters
| Parameter | Type | Description |
|---|---|---|
original | CString | The CString object to copy. |
Example
CString a = "abc";
CString b = new CString(a);int length()
Returns the number of characters in the string. The return value is always ≥ 0.
Example
CString a = "abc";
if (a.length() > 5) {
// do something
}boolean isEmpty()
Returns true if the string length is 0; false otherwise.
Example
CString a = "abc";
if (!a.isEmpty()) {
// do something
}byte byteAt(int index)
Returns the character at the specified index.
Parameters
| Parameter | Type | Description |
|---|---|---|
index | int | The position of the character to return. Must be in [0, length()-1]; otherwise an error occurs. |
Example
CString a = "abc";
byte b = a.byteAt(1); // Returns 'b'
byte c = a.byteAt(3); // Error: index out of rangeboolean equals(CString anotherString)
Returns true if the two strings contain the same sequence of characters; false otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
anotherString | CString | The string to compare. If null, a null pointer exception is triggered. |
Example
CString a = "abc";
CString b = "abc";
if (a.equals(b)) {
// do something
}boolean equalsIgnoreCase(CString anotherString)
Returns true if the two strings are identical when compared case-insensitively; false otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
anotherString | CString | The string to compare. If null, a null pointer exception is triggered. |
Example
CString a = "abc";
CString b = "ABC";
if (a.equalsIgnoreCase(b)) {
// do something
}int compareTo(CString anotherString)
Compares two strings lexicographically and returns the difference at the first character that differs.
| Return value | Meaning |
|---|---|
0 | The strings are identical. |
| Length difference | One string is a prefix of the other. |
| Character code difference | The strings differ at some position. |
Parameters
| Parameter | Type | Description |
|---|---|---|
anotherString | CString | The string to compare. |
Example
CString a = "abcde";
CString b = "abc";
CString c = "efg";
int v1 = a.compareTo(b); // Length of a minus length of b (a has b as prefix)
int v2 = a.compareTo(c); // Code difference between 'a' and 'e'
int v3 = a.compareTo("abcde"); // 0int compareToIgnoreCase(CString anotherString)
Compares two strings lexicographically, ignoring case. Converts all characters to the same case before comparing. Return value semantics are the same as compareTo.
Parameters
| Parameter | Type | Description |
|---|---|---|
anotherString | CString | The string to compare. |
Example
CString a = "abcde";
int v1 = a.compareToIgnoreCase("AbcDe"); // 0boolean startsWith(CString prefix, int toffset)
Returns true if the string starts with the given prefix when matching begins at toffset; false otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
prefix | CString | The substring to match. |
toffset | int | The index in the string from which to start matching. |
Example
CString a = "abcde";
boolean v1 = a.startsWith("bcd", 1); // true
boolean v2 = a.startsWith("bcd", 2); // falseboolean startsWith(CString prefix)
Returns true if the string starts with the given prefix; false otherwise. Equivalent to startsWith(prefix, 0).
Parameters
| Parameter | Type | Description |
|---|---|---|
prefix | CString | The substring to match. |
Example
CString a = "abcde";
boolean v1 = a.startsWith("abc"); // true
boolean v2 = a.startsWith("bcd"); // falseboolean endsWith(CString suffix)
Returns true if the string ends with the given suffix; false otherwise.
Parameters
| Parameter | Type | Description |
|---|---|---|
suffix | CString | The substring to match. |
Example
CString a = "abcde";
boolean v1 = a.endsWith("de"); // true
boolean v2 = a.endsWith("cd"); // falseint indexOf(CString str, int fromIndex)
Returns the index of the first occurrence of str in the string, searching forward from fromIndex. Returns -1 if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The substring to find. |
fromIndex | int | The index from which to start searching. |
Example
CString a = "abcdede";
int v1 = a.indexOf("de", 1); // 3
int v2 = a.indexOf("de", 6); // -1 (not found)int indexOf(CString str)
Returns the index of the first occurrence of str in the string. Returns -1 if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The substring to find. |
Example
CString a = "abcdede";
int v1 = a.indexOf("de"); // 3
int v2 = a.indexOf("fg"); // -1 (not found)int lastIndexOf(CString str, int fromIndex)
Returns the index of the last occurrence of str, searching backward from fromIndex. Returns -1 if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The substring to find. |
fromIndex | int | The index from which to search backward. |
Example
CString a = "abcdede";
int v1 = a.lastIndexOf("de", 5); // 3
int v2 = a.lastIndexOf("de", 2); // -1 (not found)int lastIndexOf(CString str)
Returns the index of the last occurrence of str in the string. Returns -1 if not found.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The substring to find. |
Example
CString a = "abcdede";
int v1 = a.lastIndexOf("de"); // 5
int v2 = a.lastIndexOf("fg"); // -1 (not found)CString substring(int beginIndex)
Returns a substring from beginIndex to the end of the string. Returns null if beginIndex is out of range.
Parameters
| Parameter | Type | Description |
|---|---|---|
beginIndex | int | The start index of the substring. Must be in [0, length()]; returns null if out of range. |
Example
CString a = "hello";
CString b = a.substring(3); // "lo"
CString c = a.substring(-1); // null (invalid index)
CString d = a.substring(7); // null (exceeds length)CString substring(int beginIndex, int endIndex)
Returns a substring from beginIndex (inclusive) to endIndex (exclusive). Returns null if either index is invalid.
Parameters
| Parameter | Type | Description |
|---|---|---|
beginIndex | int | The start index of the substring. Must be valid; returns null if out of range. |
endIndex | int | The end index of the substring (exclusive). Must be valid; returns null if out of range. |
Example
CString a = "hello";
CString b = a.substring(3, 5); // "lo"
CString c = a.substring(-1, 3); // null (invalid start index)
CString d = a.substring(0, 7); // null (end index exceeds length)CString[] split(CString str)
Splits the string into substrings using str as the delimiter. Trailing empty substrings are removed from the result.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The delimiter. |
Example
CString a = "baaaab";
CString b = "baaaa";
CString c = "aaaab";
CString[] subA = a.split("aa"); // ["b", "", "b"] — 3 substrings
CString[] subB = b.split("aa"); // ["b"] — 1 substring (trailing empty removed)
CString[] subC = c.split("aa"); // ["", "", "b"] — 3 substringsCString[] split(CString str, int limit)
Splits the string into substrings using str as the delimiter, with the result count capped at limit. If limit is 0, trailing empty substrings are removed.
Parameters
| Parameter | Type | Description |
|---|---|---|
str | CString | The delimiter. |
limit | int | The maximum number of substrings in the result. If 0, trailing empty substrings are removed. |
Example
CString a = "hello";
CString[] b = a.split("l", 1); // ["l"] — 1 substringCString trim()
Returns a copy of the string with leading and trailing spaces removed.
Example
CString a = " hello ";
CString b = a.trim(); // "hello"void getChars(byte[] dst, int dstBegin)
Copies all characters from the string into dst starting at index dstBegin. No bounds check is performed — make sure dst has enough capacity (at least length() elements available from dstBegin).
Parameters
| Parameter | Type | Description |
|---|---|---|
dst | byte[] | The destination character array. |
dstBegin | int | The index in dst at which to start writing. |
Example
byte[] dst = new byte[10];
CString a = "abc";
a.getChars(dst, 0); // Copies "abc" into dst starting at index 0void getChars(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
Copies characters from the string in the range [srcBegin, srcEnd) into dst starting at dstBegin.
Parameters
| Parameter | Type | Description |
|---|---|---|
srcBegin | int | The index of the first character to copy. |
srcEnd | int | The index after the last character to copy. |
dst | byte[] | The destination character array. |
dstBegin | int | The index in dst at which to start writing. |
Example
byte[] dst = new byte[10];
CString a = "abc";
a.getChars(0, 1, dst, 0); // Copies "a" into dst at index 0