Overview
The CString class is encapsulated in the cava.lang package. This class is used to create a string constant, for example, "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 specified character array. |
CString(CString original) | Creates a string based on a 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 specified prefix from a specified index. |
boolean startsWith(CString prefix) | Checks whether the string starts with a specified prefix. |
boolean endsWith(CString suffix) | Checks whether the string ends with a specified 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 specified delimiter. |
CString[] split(CString str, int limit) | Splits a string into substrings based on a specified delimiter. The number of substrings in the result array cannot exceed the specified limit. |
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 based on the specified start and end indexes. |
Function details
CString(byte[] value, int offset, int count)
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)
Sample code:
CString a = "abc";
CString b = new CString(a);
int length()
Sample code:
CString a = "abc";
if (a.length() > 5) {
// do something
}
boolean isEmpty()
Sample code:
CString a = "abc";
if (!a.empty()) {
// do something
}
byte byteAt(int index)
Sample code:
CString a = "abc";
byte b = a.byteAt(1);
byte b = a.byteAt(3); //invalid
boolean equals(CString anotherString)
Sample code:
CString a = "abc";
CString b = "abc";
if (a.equals(b)) {
// do something
}
boolean equalsIgnoreCase(CString anotherString)
Sample code:
CString a = "abc";
CString b = "abc";
if (a.equalsIgnoreCase(b)) {
// do something
}
int compareTo(CString anotherString)
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)
Sample code:
CString a = "abcde";
int v1 = a.compareToIgnoreCase("AbcDe"); // The value of v1 is 0.
boolean startsWith(CString prefix, int toffset)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Sample code:
CString a = "hello";
CString[] b = a.split("l", 1); // The result array b contains one string: "l".
CString trim()
Sample code:
CString a = " hello ";
CString b = a.trim(); // The result string b is "hello".
void getChars(byte[] dst, int dstBegin)
Sample code:
byte[] dst = new byte[10];
CString a = "abc";
a.getChars(dst, 0);
void getChars(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
Sample code:
byte[] dst = new byte[10];
CString a = "abc";
a.getChars(0, 1, dst, 0);