CString

更新时间:
复制 MD 格式

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

SignatureDescription
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

SignatureDescription
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

ParameterTypeDescription
valuebyte[]The character array used to create the string.
offsetintThe index of the first character. Must be ≥ 0; otherwise an error occurs.
countintThe 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

ParameterTypeDescription
originalCStringThe 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

ParameterTypeDescription
indexintThe 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 range

boolean equals(CString anotherString)

Returns true if the two strings contain the same sequence of characters; false otherwise.

Parameters

ParameterTypeDescription
anotherStringCStringThe 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

ParameterTypeDescription
anotherStringCStringThe 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 valueMeaning
0The strings are identical.
Length differenceOne string is a prefix of the other.
Character code differenceThe strings differ at some position.

Parameters

ParameterTypeDescription
anotherStringCStringThe 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"); // 0

int 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

ParameterTypeDescription
anotherStringCStringThe string to compare.

Example

CString a = "abcde";
int v1 = a.compareToIgnoreCase("AbcDe"); // 0

boolean startsWith(CString prefix, int toffset)

Returns true if the string starts with the given prefix when matching begins at toffset; false otherwise.

Parameters

ParameterTypeDescription
prefixCStringThe substring to match.
toffsetintThe 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); // false

boolean startsWith(CString prefix)

Returns true if the string starts with the given prefix; false otherwise. Equivalent to startsWith(prefix, 0).

Parameters

ParameterTypeDescription
prefixCStringThe substring to match.

Example

CString a = "abcde";
boolean v1 = a.startsWith("abc"); // true
boolean v2 = a.startsWith("bcd"); // false

boolean endsWith(CString suffix)

Returns true if the string ends with the given suffix; false otherwise.

Parameters

ParameterTypeDescription
suffixCStringThe substring to match.

Example

CString a = "abcde";
boolean v1 = a.endsWith("de"); // true
boolean v2 = a.endsWith("cd"); // false

int 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

ParameterTypeDescription
strCStringThe substring to find.
fromIndexintThe 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

ParameterTypeDescription
strCStringThe 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

ParameterTypeDescription
strCStringThe substring to find.
fromIndexintThe 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

ParameterTypeDescription
strCStringThe 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

ParameterTypeDescription
beginIndexintThe 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

ParameterTypeDescription
beginIndexintThe start index of the substring. Must be valid; returns null if out of range.
endIndexintThe 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

ParameterTypeDescription
strCStringThe 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 substrings

CString[] 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

ParameterTypeDescription
strCStringThe delimiter.
limitintThe 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 substring

CString 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

ParameterTypeDescription
dstbyte[]The destination character array.
dstBeginintThe 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 0

void 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

ParameterTypeDescription
srcBeginintThe index of the first character to copy.
srcEndintThe index after the last character to copy.
dstbyte[]The destination character array.
dstBeginintThe 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