moka.lang
Class String

java.lang.Object
  extended bymoka.lang.Object
      extended bymoka.lang.String

public class String
extends Object

The String class represents character strings. All string literals in Moka programs, such as "abc", should be implemented as instances of this class for easy manipulation.

Strings are not constant; their values can be changed after they are created. Because String objects are mutable they can't be shared. For example:

     String str = "abc";
 

is not equivalent to:

     char[] chars = {'a', 'b', 'c', '\0'};
     String str = new String(chars);
 

The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase.

String conversions are implemented through the method toString, defined by Object and inherited by all classes in Moka.

Since:
MDK1.0a

Constructor Summary
String()
          Initializes a newly created String object so that it represents an empty character sequence.
String(char_p ansiString)
          Initializes a newly created String object so that its value makes reference to a given ANSI string.
String(char_p ansiString, boolean ref)
          Initializes a newly created String object so that its value makes reference or not to a given ANSI string.
String(String original)
          Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
 
Method Summary
 String append(boolean b)
          Appends the string representation of the boolean argument to the string.
 String append(byte b)
          Appends the string representation of the byte argument to this string.
 String append(char_p str, int offset, int len)
          Appends the characters of a subarray of the char array argument to this string.
 String append(char c)
          Appends the string representation of the char argument to this string.
 String append(double d)
          Appends the string representation of the double argument to this string.
 String append(int i)
          Appends the string representation of the int argument to this string.
 String append(long l)
          Appends the string representation of the long argument to this string.
 String append(Object obj)
          Appends the string representation of the Object argument to this string.
 String append(short s)
          Appends the string representation of the short argument to this string.
 String append(String str)
          Appends the string to this string.
 char charAt(int index)
          Returns the character at the specified index.
 short compareTo(String anotherString)
          Compares two strings lexicographically.
 String concat(String str)
          Concatenates the specified string to the end of this string.
 String delete(int start, int end)
          Removes the characters in a substring of this String.
 boolean endsWith(String suffix)
          Tests if this string ends with the specified suffix.
 boolean equals(Object object)
          Compares this string to the specified object.
 boolean equalsIgnoreCase(String anotherString)
          Compares this String to another String, ignoring case considerations.
 void finalize()
          Frees the memory of system ressources used by this String object.
 void getChars(int srcBegin, int srcEnd, char_p dst, int dstBegin)
          Copies characters from this string into the destination character array.
 int indexOf(String str, int fromIndex)
          Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
 int lastIndexOf(String str, int fromIndex)
          Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.
 int length()
          Returns the length of this string.
 boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
          Tests if two string regions are equal.
 boolean regionMatches(int toffset, String other, int ooffset, int len)
          Tests if two string regions are equal.
 String replace(char oldChar, char newChar)
          Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
 String replace(String oldStr, String newStr)
          Returns a new string resulting from replacing all occurrences of oldStr substring in this string with newStr.
 boolean startsWith(String prefix, int toffset)
          Tests if this string starts with the specified prefix beginning a specified index.
 short strcmp(char_p ansiString)
          Compares two strings lexicographically.
 String substring(int beginIndex, int endIndex)
          Returns a new string that is a substring of this string.
 String toLowerCase()
          Converts all of the characters in this String to lower case.
 String toString()
          Returns a copy of this string.
 String toUpperCase()
          Converts all of the characters in this String to upper case.
 char_p toVat()
          Converts this String object to a TIOS VAT String.
 String trim()
          Returns this string, minus the leading and trailing whitespace.
static String valueOf(boolean b)
          Returns the string representation of the boolean argument.
static String valueOf(byte b)
          Returns the string representation of the byte argument.
static String valueOf(char_p ansiString)
          Returns the string representation of the ANSI string argument.
static String valueOf(char_p data, int length)
          Returns the string representation of the char array argument.
static String valueOf(char c)
          Returns the string representation of the char argument.
static String valueOf(double d)
          Returns the string representation of the double argument.
static String valueOf(int i)
          Returns the string representation of the int argument.
static String valueOf(long l)
          Returns the string representation of the long argument.
static String valueOf(short s)
          Returns the string representation of the short argument.
static String valueOf(UByte b)
          Returns the string representation of the ubyte argument.
static String valueOf(UInteger i)
          Returns the string representation of the uint argument.
static String valueOf(ULong l)
          Returns the string representation of the ulong argument.
static String valueOf(UShort s)
          Returns the string representation of the ushort argument.
 
Methods inherited from class moka.lang.Object
getClassName
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

String

public String()
Initializes a newly created String object so that it represents an empty character sequence.


String

public String(String original)
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

Parameters:
original - a String.

String

public String(char_p ansiString)
Initializes a newly created String object so that its value makes reference to a given ANSI string.

Parameters:
ansiString - an ANSI string.

String

public String(char_p ansiString,
              boolean ref)
Initializes a newly created String object so that its value makes reference or not to a given ANSI string.

Parameters:
ansiString - an ANSI string.
ref - true if the value is a reference, false otherwise.
Method Detail

append

public String append(Object obj)
Appends the string representation of the Object argument to this string.

The argument is converted to a string as if by its method toString, and the characters of that string are then appended to this string.

Parameters:
obj - an Object.
Returns:
a reference to this String object.

append

public String append(String str)
Appends the string to this string.

The characters of the String argument are appended, in order, to the contents of this string, increasing the length of this string buffer by the length of the argument. If the string object risk to be used later,use a copy of the string object instead of the original, the string being finalized before the method returns.

Parameters:
str - a string.
Returns:
a reference to this String.

append

public String append(char_p str,
                     int offset,
                     int len)
Appends the characters of a subarray of the char array argument to this string.

Characters of the character array str, starting at index offset, are appended, in order, to the contents of this string. The length of this string increases by the value of len.

Parameters:
str - the characters to be appended.
offset - the index of the first character to append.
len - the number of characters to append.
Returns:
a reference to this String object.

append

public String append(boolean b)
Appends the string representation of the boolean argument to the string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
b - a boolean.
Returns:
a reference to this String.

append

public String append(char c)
Appends the string representation of the char argument to this string.

The argument is appended to the contents of this string buffer. The length of this string buffer increases by 1.

Parameters:
c - a char.
Returns:
a reference to this String object.

append

public String append(byte b)
Appends the string representation of the byte argument to this string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
b - an byte.
Returns:
a reference to this String object.

append

public String append(short s)
Appends the string representation of the short argument to this string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
s - an short.
Returns:
a reference to this String object.

append

public String append(int i)
Appends the string representation of the int argument to this string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
i - an int.
Returns:
a reference to this String object.

append

public String append(long l)
Appends the string representation of the long argument to this string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
l - a long.
Returns:
a reference to this String object.

append

public String append(double d)
Appends the string representation of the double argument to this string.

The argument is converted to a string as if by the method String.valueOf, and the characters of that string are then appended to this string.

Parameters:
d - a double.
Returns:
a reference to this String object.

concat

public String concat(String str)
Concatenates the specified string to the end of this string.

If the length of the argument string is 0, then this String object is returned. If the value made reference to an ANSI string, this reference is not kept. Otherwise, a the value of this String object is modified, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string. In both case, the String to concatenate is finalized.

Parameters:
str - the String that is concatenated to the end of this String.
Returns:
a string that represents the concatenation of this object's characters followed by the string argument's characters.

charAt

public char charAt(int index)
Returns the character at the specified index. An index ranges from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

Parameters:
index - the index of the character.
Returns:
the character at the specified index of this string. The first character is at index 0.

delete

public String delete(int start,
                     int end)
Removes the characters in a substring of this String. The substring begins at the specified start and extends to the character at index end - 1. If start is equal to end, no changes are made.

Parameters:
start - The beginning index, inclusive.
end - The ending index, exclusive.
Returns:
This string with the characters of the specified substring removed.

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     char_p dst,
                     int dstBegin)
Copies characters from this string into the destination character array.

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

     dstbegin + (srcEnd-srcBegin) - 1
 

Parameters:
srcBegin - index of the first character in the string to copy.
srcEnd - index after the last character in the string to copy.
dst - the destination array.
dstBegin - the start offset in the destination array.

indexOf

public int indexOf(String str,
                   int fromIndex)
Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the index of the first occurrence of the specified substring. If no such value exists, then -1 is returned.

Parameters:
str - the substring for which to search.
fromIndex - the index from which to start the search.
Returns:
the index within this string of the first occurrence of the specified substring, starting at the specified index.

lastIndexOf

public int lastIndexOf(String str,
                       int fromIndex)
Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index. The integer returned is the largest value k such that:
     k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)
 
If no such value of k exists, then -1 is returned.

Parameters:
str - the substring to search for.
fromIndex - the index to start the search from.
Returns:
the index within this string of the last occurrence of the specified substring.

regionMatches

public boolean regionMatches(int toffset,
                             String other,
                             int ooffset,
                             int len)
Tests if two string regions are equal.

A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent identical character sequences. The substring of this String object to be compared begins at index toffset and has length len. The substring of other to be compared begins at index ooffset and has length len.

Parameters:
toffset - the starting offset of the subregion in this string.
other - the string argument.
ooffset - the starting offset of the subregion in the string argument.
len - the number of characters to compare.
Returns:
true if the specified subregion of this string exactly matches the specified subregion of the string argument; false otherwise.

regionMatches

public boolean regionMatches(boolean ignoreCase,
                             int toffset,
                             String other,
                             int ooffset,
                             int len)
Tests if two string regions are equal.

A substring of this String object is compared to a substring of the argument other. The result is true if these substrings represent character sequences that are the same, ignoring case if and only if ignoreCase is true. The substring of this String object to be compared begins at index toffset and has length len. The result is false otherwize. The substring of other to be compared begins at index ooffset and has length len.

Parameters:
ignoreCase - if true, ignore case when comparing characters.
toffset - the starting offset of the subregion in this string.
other - the string argument.
ooffset - the starting offset of the subregion in the string argument.
len - the number of characters to compare.
Returns:
true if the specified subregion of this string matches the specified subregion of the string argument; false otherwise. Whether the matching is exact or case insensitive depends on the ignoreCase argument.

replace

public String replace(char oldChar,
                      char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, this string object will be modified to represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Parameters:
oldChar - the old character.
newChar - the new character.
Returns:
this string with every occurrence of oldChar replaced by newChar.

replace

public String replace(String oldStr,
                      String newStr)
Returns a new string resulting from replacing all occurrences of oldStr substring in this string with newStr.

If the substring oldStr does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, this string object will be modified to represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldStr substring is replaced by an occurrence of newStr.

Returns:
this string with every occurrence of oldStr replaced by newStr.

startsWith

public boolean startsWith(String prefix,
                          int toffset)
Tests if this string starts with the specified prefix beginning a specified index.

Parameters:
prefix - the prefix.
toffset - where to begin looking in the string.
Returns:
true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise.

substring

public String substring(int beginIndex,
                        int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.
Returns:
the specified substring.

toLowerCase

public String toLowerCase()
Converts all of the characters in this String to lower case.

Returns:
the String, converted to lowercase.

toString

public String toString()
Returns a copy of this string.

Overrides:
toString in class Object
Returns:
a copy of the string.

toUpperCase

public String toUpperCase()
Converts all of the characters in this String to upper case.

Returns:
the String, converted to uppercase.

trim

public String trim()
Returns this string, minus the leading and trailing whitespace.

Returns:
This string with leading and trailing white space removed, or this string if it has no leading or trailing white space.

endsWith

public boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.

Parameters:
suffix - the suffix.
Returns:
true if the character sequence represented by the argument is a suffix of the character sequence represented by this object; false otherwise. Note that the result will be true if the argument is the empty string or is equal to this String object.

equals

public boolean equals(Object object)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides:
equals in class Object
Parameters:
object - the reference object with which to compare.
Returns:
true if the String are equal; false otherwise.

equalsIgnoreCase

public boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case.

Parameters:
anotherString - the String to compare this String against.
Returns:
true if the argument is not null and the Strings are equal, ignoring case; false otherwise.

finalize

public void finalize()
Frees the memory of system ressources used by this String object. If the value of the String do not makes reference to an ANSI String, the ANSI string memory will be freed, otherwise no.

Overrides:
finalize in class Object

compareTo

public short compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the ASCII value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:

 this.charAt(k)-anotherString.charAt(k)
 

Parameters:
anotherString - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

length

public int length()
Returns the length of this string. The length is equal to the number of characters in the string.

Returns:
the length of the sequence of characters represented by this object.

strcmp

public short strcmp(char_p ansiString)
Compares two strings lexicographically. The comparison is based on the ASCII value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument ANSI string. The result is a negative integer if this String object lexicographically precedes the argument ANSI string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal.

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, strcmp returns the difference of the two character values at position k in the two string -- that is, the value:

 this.charAt(k)-ansiString[k]
 

Parameters:
ansiString - the ANSI String to be compared.
Returns:
the value 0 if the argument ANSI string is equal to this string; a value less than 0 if this string is lexicographically less than the ANSI string argument; and a value greater than 0 if this string is lexicographically greater than the ANSI string argument.

toVat

public char_p toVat()
Converts this String object to a TIOS VAT String.

Returns:
The string converted in TIOS VAT string

valueOf

public static String valueOf(char c)
Returns the string representation of the char argument.

Parameters:
c - an char.
Returns:
a string representation of the char argument.

valueOf

public static String valueOf(boolean b)
Returns the string representation of the boolean argument.

Parameters:
b - a boolean.
Returns:
if the argument is true, a string equal to "true" is returned; otherwise, a string equal to "false" is returned.

valueOf

public static String valueOf(byte b)
Returns the string representation of the byte argument.

Parameters:
b - a byte.
Returns:
a string representation of the byte argument.

valueOf

public static String valueOf(UByte b)
Returns the string representation of the ubyte argument.

Parameters:
b - a ubyte.
Returns:
a string representation of the byte argument.

valueOf

public static String valueOf(short s)
Returns the string representation of the short argument.

Parameters:
s - an short.
Returns:
a string representation of the short argument.

valueOf

public static String valueOf(UShort s)
Returns the string representation of the ushort argument.

Parameters:
s - an ushort.
Returns:
a string representation of the ushort argument.

valueOf

public static String valueOf(int i)
Returns the string representation of the int argument.

Parameters:
i - an int.
Returns:
a string representation of the int argument.

valueOf

public static String valueOf(UInteger i)
Returns the string representation of the uint argument.

Parameters:
i - an uint.
Returns:
a string representation of the uint argument.

valueOf

public static String valueOf(long l)
Returns the string representation of the long argument.

Parameters:
l - an long.
Returns:
a string representation of the long argument.

valueOf

public static String valueOf(ULong l)
Returns the string representation of the ulong argument.

Parameters:
l - an ulong.
Returns:
a string representation of the ulong argument.

valueOf

public static String valueOf(double d)
Returns the string representation of the double argument.

Parameters:
d - an double.
Returns:
a string representation of the double argument.

valueOf

public static String valueOf(char_p ansiString)
Returns the string representation of the ANSI string argument. The contents of the character array are copied; subsequent modification of the ANSI string does not affect the newly created string.

Returns:
a newly allocated string representing the same sequence of characters contained in the ANSI string argument.

valueOf

public static String valueOf(char_p data,
                             int length)
Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

Parameters:
data - a char array.
length - the length of the char array.
Returns:
a newly allocated string representing the same sequence of characters contained in the character array argument.