All Products
Search
Document Center

OpenSearch:CString

Last Updated:Feb 06, 2023

Overview

The CString class is encapsulated in the cava.lang package. This class is used to create a string constant, such as "opensearch". CString objects cannot be modified after they are created.

Constructors

Function

Description

CString(byte[] value, int offset, int count)

Creates a string based on a specific character array.

CString(CString original)

Creates a string based on the specified CString object.

Functions

Function

Description

int length()

Returns the length of a string.

boolean isEmpty()

Checks whether a string is empty.

byte byteAt(int index)

Returns the character at the specified index of a string.

boolean equals(CString anotherString)

Checks whether two strings are the same.

boolean equalsIgnoreCase(CString anotherString)

Checks whether two strings are the same without considering the case sensitivity.

int compareTo(CString anotherString)

Compares two strings and returns the lexicographic order difference between the first different characters of the two strings.

int compareToIgnoreCase(CString anotherString)

Compares two strings and returns the lexicographic order difference between the first different characters of the two strings without considering the case sensitivity.

boolean startsWith(CString prefix, int toffset)

Checks whether the string starts with a specific prefix from a specific index.

boolean startsWith(CString prefix)

Checks whether the string starts with a specific prefix.

boolean endsWith(CString suffix)

Checks whether the string ends with a specific suffix.

int indexOf(CString str, int fromIndex)

Returns the index within the string at which the specified substring first appears from the specified index.

int indexOf(CString str)

Returns the index within the string at which the specified substring first appears.

int lastIndexOf(CString str, int fromIndex)

Returns the index within the string at which the specified substring last appears. This function checks the string from the specified end index forward.

int lastIndexOf(CString str)

Returns the index within the string at which the specified substring last appears.

CString substring(int beginIndex, int endIndex)

Returns a substring of a string based on the specified start and end indexes.

CString substring(int beginIndex)

Returns a substring of a string from the specified start index.

CString[] split(CString str)

Splits a string into substrings based on a specific delimiter.

CString[] split(CString str, int limit)

Splits a string into substrings based on a specific delimiter.

CString trim()

Removes the spaces at the start and end of a string.

void getChars(byte[] dst, int dstBegin)

Copies characters from a string into the destination character array.

void getChars(int srcBegin, int srcEnd, byte[] dst, int dstBegin)

Copies characters from a string into the destination character array.

Function details

CString(byte[] value, int offset, int count)

Creates a string based on a specific character array. A simpler format of this function is CString test = "abc". This format has the same effect as the following statements:

byte[] value = {(byte)'a', (byte)'b', (byte)'c'};
CString test = new CString(value, 0, 3);

Parameters: value: the array of characters to be used to create a string. offset: the index of the first character of the string to be created. If the value of this parameter is less than 0, an error occurs. count: the length of the string to be created. If the value of this parameter is less than 0, an error occurs.

CString(CString original)

Creates a string based on a specific CString object. Parameter: original: the CString object to be copied.

Sample code:

CString a = "abc";
CString b = new CString(a);

int length()

Returns the length of a string. Return value: the length of a string. The value is greater than or equal to 0.

Sample code:

CString a = "abc";
if (a.length() > 5) {
    // do something
}

boolean isEmpty()

Checks whether a string is empty. Return value: If the length of the string is 0, the return value is true. Otherwise, the return value is false.

Sample code:

CString a = "abc";
if (!a.empty()) {
    // do something
}

byte byteAt(int index)

Returns the character at a specific index of a string. The value of the index parameter must belong to [0,length()-1]. If a specific index is greater than the length of the string, an error occurs. Parameter: index: the index of the character to be returned. Return value: the character at a specific index.

Sample code:

CString a = "abc";
byte b = a.byteAt(1);
byte b = a.byteAt(3); //invalid

boolean equals(CString anotherString)

Checks whether two strings are the same. If the two strings are the same, this functions returns true. Otherwise, this function returns false. If the value of the anotherString parameter is a null string, a null pointer exception is triggered. Parameter: anotherString: the string to be compared. Return value: If the two strings are the same, the return value is true. Otherwise, the return value is false.

Sample code:

CString a = "abc";
CString b = "abc";
if (a.equals(b)) {
    // do something
}

boolean equalsIgnoreCase(CString anotherString)

Checks whether two strings are the same without considering the case sensitivity. If the two strings are the same after all letters are converted into uppercase letters or lowercase letters, this function returns true. Otherwise, this function returns false. If the value of the anotherString parameter is a null string, a null pointer exception is triggered. Parameter: anotherString: the string to be compared. Return value: If the two strings are the same, the return value is true. Otherwise, the return value is false.

Sample code:

CString a = "abc";
CString b = "abc";
if (a.equalsIgnoreCase(b)) {
    // do something
}

int compareTo(CString anotherString)

Compares two strings and returns the lexicographic order difference between the first different characters of the two strings. Parameter: anotherString: the string to be compared. Return value: the lexicographic order difference between the first different characters of the two strings. If the two strings are the same, the return value is 0. If one of the two strings is the prefix of the other string, the return value is the length difference between the two strings.

Sample code:

CString a = "abcde";
CString b = "abc";
CString c = "efg";
int v1 = a.compareTo(b); // The value of v1 is the length of the string a minus the length of the string b. 
int v2 = a.compareTo(c); // The value of v2 is the lexicographic order difference between the letter 'a' and the letter 'e'.
int v3 = a.compareTo("abcde"); // The value of v3 is 0.

int compareToIgnoreCase(CString anotherString)

Compares two strings and returns the lexicographic order difference between the first different characters of the two strings without considering the case sensitivity. This function converts all letters in the two strings into uppercase letters or lowercase letters before the comparison. Parameter: anotherString: the string to be compared. Return value: the lexicographic order difference between the first different characters of the two strings. If the two strings are the same, the return value is 0. If one of the two strings is the prefix of the other string, the return value is the length difference between the two strings.

Sample code:

CString a = "abcde";
int v1 = a.compareToIgnoreCase("AbcDe"); // The value of v1 is 0.

boolean startsWith(CString prefix, int toffset)

Checks whether the string starts with a specific prefix from a specific index. Parameters: prefix: the substring to be matched. toffset: the start index from which the substring is to be matched. Return value: If the string starts with the specified prefix from the specified index, the return value is true. Otherwise, the return value is false.

Sample code:

CString a = "abcde";
boolean v1 = a.startsWith("bcd", 1); // The value of v1 is true.
boolean v2 = a.startsWith("bcd", 2); // The value of v2 is false.

boolean startsWith(CString prefix)

Checks whether the string starts with a specific prefix. This function has the same effect as setting the toffset parameter to 0 in the `boolean startsWith(CString prefix, int toffset)` function. Parameter: prefix: the substring to be matched. Return value: If the string starts with the specified prefix, the return value is true. Otherwise, the return value is false.

Sample code:

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

boolean endsWith(CString suffix)

Checks whether the string ends with a specific suffix. Parameter: suffix: the substring to be matched. Return value: If the string starts with the specified suffix, the return value is true. Otherwise, the return value is false.

Sample code:

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

int indexOf(CString str, int fromIndex)

Returns the index within the string at which the specified substring first appears from the specified index. Parameters: str: the substring to be matched. fromIndex: the start index from which the substring is to be matched. Return value: the index within the string at which the specified substring first appears from the specified index. If the substring does not appear, the return value is -1.

Sample code:

CString a = "abcdede";
int v1 = a.indexOf("de", 1); // The value of v1 is 3.
int v2 = a.indexOf("de", 6); // The value of v2 is -1.

int indexOf(CString str)

Returns the index within the string at which the specified substring first appears. Parameter: str: the substring to be matched. Return value: the index within the string at which the specified substring first appears. If the substring does not appear, the return value is -1.

Sample code:

CString a = "abcdede";
int v1 = a.indexOf("de"); // The value of v1 is 3.
int v2 = a.indexOf("fg"); // The value of v2 is -1.

int lastIndexOf(CString str, int fromIndex)

Returns the index within the string at which the specified substring last appears. This function checks the string from the specified end index forward. Parameters: str: the substring to be matched. fromIndex: the index from which the substring is to be matched forward. Return value: the index within the string at which the specified substring last appears. If the substring does not appear, the return value is -1.

Sample code:

CString a = "abcdede";
int v1 = a.lastIndexOf("de", 5); // The value of v1 is 3.
int v2 = a.lastIndexOf("de", 2); // The value of v2 is -1.

int lastIndexOf(CString str)

Returns the index within the string at which the specified substring last appears. This function checks the string from the specified end index forward. Parameters: str: the substring to be matched. fromIndex: the index from which the substring is to be matched forward. Return value: the index within the string at which the specified substring last appears. If the substring does not appear, the return value is -1.

Sample code:

CString a = "abcdede";
int v1 = a.lastIndexOf("de"); // The value of v1 is 5.
int v2 = a.lastIndexOf("fg"); // The value of v2 is -1.

CString substring(int beginIndex)

Returns a substring of a string from the specified start index. Parameter: beginIndex: the start index of the substring to be returned. Return value: the substring of the string from the specified start index. If the specified start index is less than 0 or greater than the length of the string, the return value is null.

Sample code:

CString a = "hello";
CString b = a.substring(3); // The value of b is "lo".
CString c = a.substring(-1); // The value of c is null.
CString d = a.substring(7); // The value of d is null.

CString substring(int beginIndex, int endIndex)

Returns a substring of a string based on the specified start and end indexes. Parameters: beginIndex: the start index of the substring to be returned. endIndex: the end index of the substring to be returned. Return value: the substring of the string based on the specified start and end indexes. If the specified start index or end index is invalid, the return value is null.

Sample code:

CString a = "hello";
CString b = a.substring(3, 5); // The value of b is "lo".
CString c = a.substring(-1, 3); // The value of b is null.
CString d = a.substring(0, 7); // The value of d is null.

CString[] split(CString str)

Splits a string into substrings based on a specific delimiter. Empty substrings at the end of the result array are removed. Parameter: str: the delimiter to be used to split the string. Return value: the array of substrings that are split based on the specified delimiter.

Sample code:

CString a = "baaaab";
CString b = "baaaa";
CString c = "aaaab";
CString[] subA = a.split("aa"); // The result array subA contains three substrings: "b", "", and "b".
CString[] subB = b.split("aa"); // The result array subB contains one substring: "b".
CString[] subC = c.split("aa"); // The result array subC contains three substrings: "", "", and "b".

CString[] split(CString str, int limit)

Splits a string into substrings based on a specific delimiter. The number of substrings in the result array cannot exceed the specified limit. If the specified limit is 0, empty substrings at the end of the result array are removed. Parameter: str: the delimiter. limit: the maximum number of substrings in the result array. Return value: the array of substrings that are split based on the specified delimiter.

Sample code:

CString a = "hello";
CString[] b = a.split("l", 1); // The result array b contains one string: "l".

CString trim()

Removes the spaces at the start and end of a string. Return value: the string after the spaces at the start and end are removed.

Sample code:

CString a = " hello  ";
CString b = a.trim(); // The result string b is "hello".

void getChars(byte[] dst, int dstBegin)

Copies characters from a string into the destination character array. This function does not perform a validity check. Make sure that the length of the destination character array is greater than or equal to that of the string. Parameters: dst: the destination character array. dstBegin: the index within the destination character array from which the string is to be copied.

Sample code:

byte[] dst = new byte[10];
CString a = "abc";
a.getChars(dst, 0);

void getChars(int srcBegin, int srcEnd, byte[] dst, int dstBegin)

Copies characters from a string into the destination character array based on the specified start and end indexes. Parameters: srcBegin: the first character of the string to be copied. srcEnd: the last character of the string to be copied. dst: the destination character array. dstBegin: the index within the destination character array from which the string is to be copied.

Sample code:

byte[] dst = new byte[10];
CString a = "abc";
a.getChars(0, 1, dst, 0);