aicas logoJamaica 6.4 release 1

java.lang
Class String

java.lang.Object
  extended by java.lang.String
All Implemented Interfaces:
Serializable, CharSequence, Comparable<String>

public final class String
extends Object
implements Serializable, Comparable<String>, CharSequence

String represents immutable strings of (16-bit) unicode characters.

Supplementary characters are represented by pairs of 16-bit char values (surrogate pairs), i.e., UTF-16 encoding is used. Strings provide access to the single 16-bit char in the UTF-16 string, but also to the unicode code points.

Since:
1.0
See Also:
Serialized Form

Field Summary
static Comparator<String> CASE_INSENSITIVE_ORDER
          A comparator that is based on compareToIgnoreCase to order strings.
 
Constructor Summary
String()
          Constructor for an empty string.
String(byte[] bytes)
          Constructor that creates a String object from the given byte data using the platform's default encoding.
String(byte[] bytes, Charset charset)
          Constructs a new String by decoding the specified array of bytes using the specified charset.
String(byte[] value, int hibyte)
          Deprecated. This constructor does not use a proper encoding. Use constructor String(byte[],String) instead.
String(byte[] bytes, int offset, int count)
          Constructor that creates a String object from the given byte data using the platform's default encoding.
String(byte[] bytes, int offset, int length, Charset charset)
          Constructs a new String by decoding the specified subarray of bytes using the specified charset.
String(byte[] value, int hibyte, int offset, int count)
          Deprecated. This constructor does not use a proper encoding. Use constructor String(byte[],int,int,String) instead.
String(byte[] bytes, int offset, int count, String enc)
          Create a new String with the contents of the given byte-array converted to 16-bit unicode using the given encoding.
String(byte[] bytes, String enc)
          Create a new String with the contents of the given byte-array converted to 16-bit unicode using the given encoding.
String(char[] value)
          Constructor for a string that contains the characters of a given character array.
String(char[] value, int offset, int count)
          Constructor for a string that contains a subrange of the characters of a given character array.
String(int[] codePoints, int offset, int count)
          Constructor that creates a string object from the unicode code points in an integer array.
String(String str)
          Constructor for a copy of a string.
String(StringBuffer sb)
          Constructor that creates a String object from the characters in a StringBuffer.
String(StringBuilder sb)
          Constructor that creates a String object from the characters in a StringBuilder.
 
Method Summary
 char charAt(int index)
          charAt returns the 16-bit character at the given index of this String.
 int codePointAt(int index)
          codePointAt
 int codePointBefore(int index)
          codePointBefore
 int codePointCount(int beginIndex, int endIndex)
          codePointCount
 int compareTo(String str)
          compareTo compares two strings lexicographically.
 int compareToIgnoreCase(String str)
          compareTo compares two strings lexicographically ignoring case.
 String concat(String str)
          concat creates a String that contains the concatenation of this string and another string.
 boolean contains(CharSequence s)
          Check if this string contains the given CharSequence.
 boolean contentEquals(CharSequence cs)
          Compares the content of the given CharSequence to this String.
 boolean contentEquals(StringBuffer sb)
          Compares the content of the given StringBuffer to this String.
static String copyValueOf(char[] data)
          copyValueOf returns a String containing the characters of the given character array.
static String copyValueOf(char[] data, int offset, int count)
          copyValueOf creates a String that contains a subrange of the characters of a given character array.
 boolean endsWith(String suffix)
          Check if this string ends with the given suffix.
 boolean equals(Object o)
          Compare this String to another String.
 boolean equalsIgnoreCase(String str)
          Compare this String to another String ignoring case.
static String format(Locale l, String format, Object... args)
          Returns a formatted string using the specified locale, format string, and arguments.
static String format(String format, Object... args)
          Returns a formatted string using the specified format string and arguments.
 byte[] getBytes()
          getBytes converts the characters of this String to a sequence of bytes using the default encoding.
 byte[] getBytes(Charset encoding)
          Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
 void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
          Deprecated. This method does not use the proper encoding. Use getBytes() to use the default encoding.
 byte[] getBytes(String encoding)
          getBytes converts the characters of this String to a sequence of bytes using the specified encoding.
 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
          getChars copies a subrange of the the characters of this string into a character array.
 int hashCode()
          hashCode calculates a hash code for this string.
 int indexOf(int ch)
          Find the first index of a given character or code point.
 int indexOf(int ch, int from)
          Find the first index of a given character or code point starting from a given findex.
 int indexOf(String str)
          Return the index of the first substring of this that is equal to str.
 int indexOf(String str, int from)
          Return the index of the first substring of this that is equal to str and that start at from or later.
protected  int indexOfCharSequence(CharSequence s, int from)
          Return the index of the first occurrence of a substring this that is equal to a CharSequence and that starts at from or later.
 String intern()
          intern interns this String.
 boolean isEmpty()
          Returns true if, and only if, length() is 0.
 int lastIndexOf(int ch)
          Find the last index of a given character or code point.
 int lastIndexOf(int ch, int from)
          Find the last index of a given character or code point starting from a given findex.
 int lastIndexOf(String str)
          Return the index of the last substring of this that is equal to str.
 int lastIndexOf(String str, int from)
          Return the index of the last substring of this that is equal to str and that is before a given index.
 int length()
          length returns the length of this string, i.e., the number of 16-bit characters contained in this string.
 boolean matches(String regex)
          Test if this String matches the given regular expression.
 int offsetByCodePoints(int index, int codePointOffset)
          offsetByCodePoints
 boolean regionMatches(boolean ignoreCase, int offset, String other, int ooffset, int len)
          Compare a region of this string with another string, optionally ignoring case.
 boolean regionMatches(int offset, String other, int ooffset, int len)
          Compare a region of this string with another string.
 String replace(char oldChar, char newChar)
          replace creates a new String that is equal to this String with a given character replaced by another character.
 String replace(CharSequence target, CharSequence replacement)
          Replaces all occurrences of target in this by te given replacement.
 String replaceAll(String regex, String replacement)
          Replaces all occurrences of the substring that matches the given regular expression.
 String replaceFirst(String regex, String replacement)
          Replaces the first substring that matches the given reugular expression.
 String[] split(String regex)
          split splits the string at substrings that match the given regular expression.
 String[] split(String regex, int limit)
          split splits the string at substrings that match the given regular expression.
 boolean startsWith(String prefix)
          Check if this string starts with the given prefix.
 boolean startsWith(String prefix, int offset)
          Check if this string starts with the given prefix from the given offset.
 CharSequence subSequence(int start, int end)
          subSequence creates a subsequence of this sequence.
 String substring(int from)
          Create a new String object with the contents of the suffix of this String starting at a given index.
 String substring(int from, int end)
          Create a new String objects with the contents of the substring of this String.
 char[] toCharArray()
          toCharArray allocates a new character array and copies the characters of this String into the array.
 String toLowerCase()
          toLowerCase converts this String to lower case using the default locale.
 String toLowerCase(Locale locale)
          toLowerCase converts this String to lower case respecting the given locale.
 String toString()
          toString creates a printable string that represents this object for debugging purposes.
 String toUpperCase()
          toUpperCase converts this String to lower case using the default locale.
 String toUpperCase(Locale locale)
          toLowerCase converts this String to upper case respecting the given locale.
 String trim()
          trim removes all leading and trailing characters with a code <= ' ' from this String.
static String valueOf(boolean b)
          valueOf converts a boolean to a String.
static String valueOf(char c)
          valueOf creates a one-character string containing the given character.
static String valueOf(char[] data)
          valueOf returns a String containing the characters of the given character array.
static String valueOf(char[] data, int offset, int count)
          valueOf creates a String that contains a subrange of the characters of a given character array.
static String valueOf(double d)
          valueOf converts a double to a String
static String valueOf(float f)
          valueOf converts a float to a String
static String valueOf(int i)
          valueOf converts an int to a String.
static String valueOf(long l)
          valueOf converts a long to a String
static String valueOf(Object obj)
          valueOf returns a String created from a given object or null.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

CASE_INSENSITIVE_ORDER

public static final Comparator<String> CASE_INSENSITIVE_ORDER
A comparator that is based on compareToIgnoreCase to order strings.

This comparator is serializable.

Since:
1.2
Constructor Detail

String

public String()
Constructor for an empty string.

A more efficient way to obtain an empty string is by using the string constant "". Using this constructor will always create a new instance, i.e., perform a memory allocation.


String

public String(String str)
Constructor for a copy of a string.

Using this constructor will always create a new instance, i.e., perform a memory allocation. Unless a different instance is required, copying of a string is not needed.

Parameters:
str - string to copy
Throws:
NullPointerException - if str is null.

String

public String(char[] value)
Constructor for a string that contains the characters of a given character array.

ensure

   (   length() == value.length
    && for i in 0..value.length-1: charAt(i) == value[i])
 

Parameters:
value - character array
Throws:
NullPointerException - if value is null.

String

public String(char[] value,
              int offset,
              int count)
Constructor for a string that contains a subrange of the characters of a given character array.

ensure

   (   length() == count
    && for i in 0..count-1: charAt(i) == value[offset+i])
 

Parameters:
value - the character array
offset - the index of the first character to use in the string
count - the number of characters to use in the string.
Throws:
NullPointerException - if value is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>value.length.

String

@Deprecated
public String(byte[] value,
                         int hibyte,
                         int offset,
                         int count)
Deprecated. This constructor does not use a proper encoding. Use constructor String(byte[],int,int,String) instead.

Create a new String with the content of the given byte-array and the fixed hibyte for each character.

ensure

   (   length() == count
    && for i in 0..count-1: charAt(i) == ((hibyte & 0xff) << 8 + value[offset+i]))
 

Parameters:
value - the buffer to read from.
hibyte - fixed hibate for all characters.
offset - the index of the first character in value to use in the string
count - number of bytes to copy over.
Throws:
NullPointerException - if value is null
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>value.length.

String

@Deprecated
public String(byte[] value,
                         int hibyte)
Deprecated. This constructor does not use a proper encoding. Use constructor String(byte[],String) instead.

Create a new String with the content of the given byte-array and the fixed hibyte for each character.

ensure

   (   length() == value.length
    && for i in 0..length()-1: charAt(i) == ((hibyte & 0xff) << 8 + value[i]))
 

Parameters:
value - the buffer to read from.
hibyte - fixed hibate for all characters.
Throws:
NullPointerException - if value is null

String

public String(byte[] bytes,
              int offset,
              int count,
              String enc)
       throws UnsupportedEncodingException
Create a new String with the contents of the given byte-array converted to 16-bit unicode using the given encoding.

Parameters:
bytes - the byte array to read from
offset - offset of the first byte to used from bytes.
count - number of bytes to use from bytes.
enc - the encoding to be used.
Throws:
UnsupportedEncodingException - if the specified encoding is not supported.
NullPointerException - if bytes is null or enc is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>value.length.
Since:
1.1

String

public String(byte[] bytes,
              int offset,
              int length,
              Charset charset)
Constructs a new String by decoding the specified subarray of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the subarray.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

Parameters:
bytes - The bytes to be decoded into characters
offset - The index of the first byte to decode
length - The number of bytes to decode
charset - The charset to be used to decode the bytes
Throws:
IndexOutOfBoundsException - If the offset and length arguments index characters outside the bounds of the bytes array
Since:
1.6

String

public String(byte[] bytes,
              Charset charset)
Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

Parameters:
bytes - The bytes to be decoded into characters
charset - The charset to be used to decode the bytes
Since:
1.6

String

public String(byte[] bytes,
              String enc)
       throws UnsupportedEncodingException
Create a new String with the contents of the given byte-array converted to 16-bit unicode using the given encoding.

Parameters:
bytes - the byte array to read from
enc - the encoding to be used.
Throws:
UnsupportedEncodingException - if the specified encoding is not supported.
NullPointerException - if bytes is null or enc is null.
Since:
1.1

String

public String(byte[] bytes,
              int offset,
              int count)
Constructor that creates a String object from the given byte data using the platform's default encoding.

For JamaicaVM, the default encoding is assumed to be 8-bit ASCII.

Parameters:
bytes - the byte array to read from
offset - the start index in bytes
count - the number of bytes to be used in the new String
Throws:
NullPointerException - if bytes is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>bytes.length.
Since:
1.1

String

public String(byte[] bytes)
Constructor that creates a String object from the given byte data using the platform's default encoding.

For JamaicaVM, the default encoding is assumed to be 8-bit ASCII.

Parameters:
bytes - the byte array to read from
Throws:
NullPointerException - if bytes is null.
Since:
1.1

String

public String(StringBuffer sb)
Constructor that creates a String object from the characters in a StringBuffer.

ensure

   (   length() == sb.length()
    && for i in 0..length()-1: charAt(i) == sb.charAt(i))
 

Parameters:
sb - the string buffer to take the characters from
Throws:
NullPointerException - if sb is null.

String

public String(StringBuilder sb)
Constructor that creates a String object from the characters in a StringBuilder.

ensure

   (   length() == sb.length()
    && for i in 0..length()-1: charAt(i) == sb.charAt(i))
 

Parameters:
sb - the string builder to take the characters from
Throws:
NullPointerException - if sb is null.
Since:
1.5

String

public String(int[] codePoints,
              int offset,
              int count)
Constructor that creates a string object from the unicode code points in an integer array. Supplementary characters will be encoded using surrogate pairs using UTF-16 encoding.

Parameters:
codePoints - an array of unicode code points
offset - the offset of the first code point to use
count - the number of code points to use
Throws:
IllegalArgumentException - if codePoints[offset..offset+count] contains invalid code points
NullPointerException - if codePoints is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>bytes.length.
Since:
1.5
Method Detail

length

public int length()
length returns the length of this string, i.e., the number of 16-bit characters contained in this string.

ensure

   (result >= 0)
 

Specified by:
length in interface CharSequence
Returns:
the length.

charAt

public char charAt(int index)
charAt returns the 16-bit character at the given index of this String.

implemented in C for better performance.

Specified by:
charAt in interface CharSequence
Parameters:
index - the position of the character to the read
Returns:
the character at position index
Throws:
IndexOutOfBoundsException - if index<0 or index >= length().

codePointAt

public int codePointAt(int index)
codePointAt

Parameters:
index -
Returns:
The code point value of the character at the index.
Since:
1.5

codePointBefore

public int codePointBefore(int index)
codePointBefore

Parameters:
index -
Returns:
The Unicode code point value before the given index.
Since:
1.5

codePointCount

public int codePointCount(int beginIndex,
                          int endIndex)
codePointCount

Parameters:
beginIndex -
endIndex -
Returns:
The number of Unicode code points in the specified text range.
Since:
1.5

offsetByCodePoints

public int offsetByCodePoints(int index,
                              int codePointOffset)
offsetByCodePoints

Parameters:
index -
codePointOffset -
Returns:
The index within this String.
Since:
1.5

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     char[] dst,
                     int dstBegin)
getChars copies a subrange of the the characters of this string into a character array.

ensure

   (for i in 0..srcEnd-srcBegin-1: charAt(srcBegin+i) == dst[dstBegin+i])
 

Parameters:
srcBegin - the index of the first character to copy
srcEnd - the index of the last character to copy
dst - (out!) the target character array
dstBegin - the index in the target array at which the first character should be stored.
Throws:
IndexOutOfBoundsException - if srcBegin<0, srcBegin>srcEnd, srcEnd > length(), dstBegin<0 or dstBegin+srcEnd-srcBegin > dst.length;
NullPointerException - if dst is null

getBytes

@Deprecated
public void getBytes(int srcBegin,
                                int srcEnd,
                                byte[] dst,
                                int dstBegin)
Deprecated. This method does not use the proper encoding. Use getBytes() to use the default encoding.

getBytes copies a subrange of the the characters of this string into a byte array. The 16-bit characters are converted to 8-bit bytes by erasing the upper 8 bits.

ensure

   (for i in 0..srcEnd-srcBegin-1: (charAt(srcBegin+i)&0xff) == (dst[dstBegin+i]&0xff))
 

Parameters:
srcBegin - the index of the first character to copy
srcEnd - the index of the last character to copy
dst - (out!) the target byte array
dstBegin - the index in the target array at which the first character should be stored.
Throws:
IndexOutOfBoundsException - if srcBegin<0, srcBegin>srcEnd, srcEnd > length(), dstBegin<0 or dstBegin+srcEnd-srcBegin > dst.length;
NullPointerException - if dst is null

getBytes

public byte[] getBytes(String encoding)
                throws UnsupportedEncodingException
getBytes converts the characters of this String to a sequence of bytes using the specified encoding. The bytes are then stored into a newly allocated byte array that is returned as the result of this method.

Characters that cannot be encoded with the specified encoding will be replaced by '?'.

Parameters:
encoding - the encoding to be used, null for the default encoding (ISO 8859-1 for JamaicaVM)
Returns:
the new byte array
Throws:
UnsupportedEncodingException - if the specified encoding is not supported.
Since:
1.1

getBytes

public byte[] getBytes()
getBytes converts the characters of this String to a sequence of bytes using the default encoding. The bytes are then stored into a newly allocated byte array that is returned as the result of this method.

Characters that cannot be encoded with the default encoding will be replaced by '?'.

For JamaicaVM, the default encoding is ISO 8859-1.

Returns:
the new byte array
Since:
1.1

getBytes

public byte[] getBytes(Charset encoding)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.

Parameters:
encoding - The Charset to be used to encode the String
Returns:
The resultant byte array
Since:
1.6

equals

public boolean equals(Object o)
Compare this String to another String.

ensure

   (result == (   (anObject instanceof String)
               && (length() == ((String)o).length())
               && (for all i in 0..length()-1: charAt(i) == ((String)o).charAt(i)))
 

implemented in C for better performance.

Overrides:
equals in class Object
Parameters:
o - object
Returns:
true if this and o are equal strings.

contentEquals

public boolean contentEquals(StringBuffer sb)
Compares the content of the given StringBuffer to this String.

Parameters:
sb - the StringBuffer to compare.
Returns:
true if the data content of the StringBuffer equals this String.
Throws:
NullPointerException - if sb is null
Since:
1.4

contentEquals

public boolean contentEquals(CharSequence cs)
Compares the content of the given CharSequence to this String.

Parameters:
cs - the CharSequence to compare with.
Returns:
true if the data content of the CharSequence equals this String.
Throws:
NullPointerException - if cs is null
Since:
1.5

equalsIgnoreCase

public boolean equalsIgnoreCase(String str)
Compare this String to another String ignoring case. Two characters c1 and c2 are considered equal if

      (c1 == c2)
   || Character.toUpperCase(c1) == Character.toUpperCase(c2)
   || Character.toLowerCase(c1) == Character.toLowerCase(c2)
 

Parameters:
str - the string to compare to
Returns:
true if str!=null, length()==str.length() and all characters of this and str are equal ignoring case.

compareTo

public int compareTo(String str)
compareTo compares two strings lexicographically. The result is 0 if both strings are equal. Else, if the first character at which this and str differ is at index i, the result is charAt(i) - str.charAt(i). If both strings differ in length and are equal up to the last character of the shorter string, the result is length() - str.length().

implemented in C for better performance.

Specified by:
compareTo in interface Comparable<String>
Parameters:
str - a string to compare to.
Returns:
0 if strings are equal, < 0 if this < str, > 0 if this > str.
Throws:
NullPointerException - if str==null

compareToIgnoreCase

public int compareToIgnoreCase(String str)
compareTo compares two strings lexicographically ignoring case. The result is 0 if both strings are equal. Else, the result is < 0 or > 0, respectively, depending on whether this < str or this > str.

Case is ignored by converting each character c via nested calls to Character.toLowerCase(Character.toUpperCase(c)) before the comparison.

Parameters:
str - a string to compare to.
Returns:
0 if strings are equal, < 0 if this < str, > 0 if this > str.
Throws:
NullPointerException - if str==null
Since:
1.2

regionMatches

public boolean regionMatches(int offset,
                             String other,
                             int ooffset,
                             int len)
Compare a region of this string with another string.

Parameters:
offset - the index in this at which comparison should start.
other - the other String to compare with
ooffset - the index in other at which comparison should start.
len - the number of characters to compare.
Returns:
true iff for all i in 0..len-1: charAt(offset+i) == other.charAt(ooffset+i).
Throws:
NullPointerException - if other is null.

regionMatches

public boolean regionMatches(boolean ignoreCase,
                             int offset,
                             String other,
                             int ooffset,
                             int len)
Compare a region of this string with another string, optionally ignoring case. If ignoreCase==false, this is equal to regionMatches(offset,other,ooffset,len). Otherwise, two characters c1 and c2 are considered equal iff

      (c1 == c2)
   || Character.toUpperCase(c1) == Character.toUpperCase(c2)
   || Character.toLowerCase(c1) == Character.toLowerCase(c2)
 

Parameters:
ignoreCase - true to ignore case, false to compare characters directly.
offset - the index in this at which comparison should start.
other - the other String to compare with
ooffset - the index in other at which comparison should start.
len - the number of characters to compare.
Returns:
true iff for all i in 0..len-1: charAt(offset+i) equals (ignoring case if ignoreCase==true) other.charAt(ooffset+i).
Throws:
NullPointerException - if other is null.

startsWith

public boolean startsWith(String prefix,
                          int offset)
Check if this string starts with the given prefix from the given offset.

Parameters:
prefix - the prefix
offset - the offset in this where to start looking for the prefix
Returns:
true if for all i in 0..prefix.length()-1: charAt(offset+i) == prefix.charAt(i).
Throws:
NullPointerException - if prefix is null.

startsWith

public boolean startsWith(String prefix)
Check if this string starts with the given prefix.

Parameters:
prefix - the prefix
Returns:
true if for all i in 0..prefix.length()-1: charAt(i) == prefix.charAt(i).
Throws:
NullPointerException - if prefix is null.
Since:
1.0

endsWith

public boolean endsWith(String suffix)
Check if this string ends with the given suffix.

Parameters:
suffix - the suffix
Returns:
true if for all i in 0..suffix.length()-1: charAt(length()-suffix.length()+i) == suffix.charAt(i).
Throws:
NullPointerException - if suffix is null.

hashCode

public int hashCode()
hashCode calculates a hash code for this string. The hash code value calculations takes into account each single character. The value is

   charAt(0)*31**(length()-1) + charAt(1)*31**(length()-2) + ... + charAt(length()-1)
 

implemented in C for better performance.

Overrides:
hashCode in class Object
Returns:
the hashCode value

indexOf

public int indexOf(int ch)
Find the first index of a given character or code point.

ensure

   (   ((ch <= 0xffff && result == -1)
           IMPLIES for all i=0..length()-1: charAt(i) != ch)
    && ((ch <= 0xffff && result != -1)
           IMPLIES (   (charAt(result) == ch)
                    && for all i=0..result-1: charAt(i) != ch))
    && ((ch >  0xffff && result == -1)
           IMPLIES for all codepoint indices i: codePointAt(i) != ch)
    && ((ch >  0xffff && result != -1)
           IMPLIES (   (codePointAt(result) == ch)
                    && for all i=0..result-1: codePointAt(i) != ch)
 

Parameters:
ch - the character or code point
Returns:
the first index of ch in this String, -1 if ch does not occur in this string.

indexOf

public int indexOf(int ch,
                   int from)
Find the first index of a given character or code point starting from a given findex.

ensure

   (   ((ch <= 0xffff && result == -1)
           IMPLIES for all i=from..length()-1: charAt(i) != ch)
    && ((ch <= 0xffff && result != -1)
           IMPLIES (   (charAt(result) == ch)
                    && for all i=from..result-1: charAt(i) != ch))
    && ((ch >  0xffff && result == -1)
           IMPLIES for all codepoint indices i: codePointAt(i) != ch)
    && ((ch >  0xffff && result != -1)
           IMPLIES (   (codePointAt(result) == ch)
                    && for all i=from..result-1: codePointAt(i) != ch)
 

Parameters:
ch - the character or code point
Returns:
the first index of ch in this String, -1 if ch does not occur in this string.

lastIndexOf

public int lastIndexOf(int ch)
Find the last index of a given character or code point.

ensure

   (   ((ch <= 0xffff && result == -1)
           IMPLIES for all i=0..length()-1: charAt(i) != ch)
    && ((ch <= 0xffff && result != -1)
           IMPLIES (   (charAt(result) == ch)
                    && for all i=result+1..length()-1: charAt(i) != ch))
    && ((ch >  0xffff && result == -1)
           IMPLIES for all codepoint indices i: codePointAt(i) != ch)
    && ((ch >  0xffff && result != -1)
           IMPLIES (   (codePointAt(result) == ch)
                    && for all codepoint indices in i=result+1..length()-1: codePointAt(i) != ch)
 

Parameters:
ch - the character or code point
Returns:
the last index of ch in this String, -1 if ch does not occur in this string.

lastIndexOf

public int lastIndexOf(int ch,
                       int from)
Find the last index of a given character or code point starting from a given findex.

ensure

   (   ((ch <= 0xffff && result == -1)
           IMPLIES for all i=0..from: charAt(i) != ch)
    && ((ch <= 0xffff && result != -1)
           IMPLIES (   (charAt(result) == ch)
                    && for all i=0..from: charAt(i) != ch))
    && ((ch >  0xffff && result == -1)
           IMPLIES for all codepoint indices i in 0..from: codePointAt(i) != ch)
    && ((ch >  0xffff && result != -1)
           IMPLIES (   (codePointAt(result) == ch)
                    && for all i=0..from: codePointAt(i) != ch)
 

Parameters:
ch - the character or code point
Returns:
the first index of ch in this String, -1 if ch does not occur in this string.

indexOf

public int indexOf(String str)
Return the index of the first substring of this that is equal to str.

Parameters:
str - the required substring
Returns:
the index in this of the first substring found, -1 if this does not contain str as a substring.
Throws:
NullPointerException - if str is null.

indexOf

public int indexOf(String str,
                   int from)
Return the index of the first substring of this that is equal to str and that start at from or later.

ensure

   (result != -1 IMPLIES startWith(str,result));
 

implemented in C for better performance.

Parameters:
str - the required substring
from - the index to start to search for the substring.
Returns:
the index in this of the first substring found, -1 if this.substring(from) does not contain str as a substring.
Throws:
NullPointerException - if str is null.

lastIndexOf

public int lastIndexOf(String str)
Return the index of the last substring of this that is equal to str.

ensure

   (result != -1 IMPLIES startsWith(str,result) && result <= length());
 

Parameters:
str - the required substring
Returns:
the index in this of the last substring found, -1 if this does not contain str as a substring.
Throws:
NullPointerException - if str is null.

lastIndexOf

public int lastIndexOf(String str,
                       int from)
Return the index of the last substring of this that is equal to str and that is before a given index.

ensure

   (result != -1 IMPLIES startsWith(str,result) && result <= from && result <= length());
 

Parameters:
str - the required substring
from - the starting index.
Returns:
the index in this of the last substring found, -1 if this does not contain str as a substring.
Throws:
NullPointerException - if str is null.

substring

public String substring(int from)
Create a new String object with the contents of the suffix of this String starting at a given index.

ensure

   (   (result.length() == length()-from)
    && (for all i=0..result.length(): result.charAt(i) == charAt(from+i)));
 

Parameters:
from - the starting index.
Returns:
a new String with the suffix of this starting at from.
Throws:
IndexOutOfBoundsException - if from<0 or from>length().

substring

public String substring(int from,
                        int end)
Create a new String objects with the contents of the substring of this String.

ensure

   (   (result.length() == end-from)
    && (for all i=0..result.length(): result.charAt(i) == charAt(from+i)));
 

Parameters:
from - the starting index (inclusive).
end - the end index (exclusive)..
Returns:
a new String with the the substring [from..end), or this if the substring would be equal to this.
Throws:
IndexOutOfBoundsException - if from<0 or end>length() or end

subSequence

public CharSequence subSequence(int start,
                                int end)
subSequence creates a subsequence of this sequence.

This implementation of the interface method CharSequence.subSequence behaves exactly as substring(start,end).

Specified by:
subSequence in interface CharSequence
Parameters:
start - the start position.
end - the end position.
Returns:
a subsequence
Throws:
IndexOutOfBoundsException - if start<0 || endlength().
Since:
1.4

contains

public boolean contains(CharSequence s)
Check if this string contains the given CharSequence.

Parameters:
s - a character sequence
Returns:
indexOf(s.toString())>=0
Throws:
NullPointerException - if s is null
Since:
1.5

indexOfCharSequence

protected int indexOfCharSequence(CharSequence s,
                                  int from)
Return the index of the first occurrence of a substring this that is equal to a CharSequence and that starts at from or later.

ensure

   (result != -1 IMPLIES startsWith(s.toString(),result));
 

Parameters:
s - the required substring
from - the index to start to search for the substring.
Returns:
the index in this of the first substring found, -1 if this.substring(from) does not contain str as a substring.
Throws:
NullPointerException - if s is null.

replace

public String replace(CharSequence target,
                      CharSequence replacement)
Replaces all occurrences of target in this by te given replacement.

Parameters:
target - the strings to replace
replacement - the string target should be replaced with
Returns:
a string created from this by replacing all occurrences of target with replacement. this is returned if !this.contains(target).
Throws:
NullPointerException - if target or replacement is null.
Since:
1.5

concat

public String concat(String str)
concat creates a String that contains the concatenation of this string and another string.

For JamaicaVM, this returns this or str if str.length()==0 or this.length()==0, respectively.

ensure

   (   (result.length() == this.length()+str.length())
    && (for all i=0..this.length(): result.charAt(i         ) ==     charAt(i))
    && (for all i=0..str .length(): result.charAt(length()+i) == str.charAt(i)))
 

Parameters:
str - the other string
Returns:
the concatenation of this and str.
Throws:
NullPointerException - if str is null

replace

public String replace(char oldChar,
                      char newChar)
replace creates a new String that is equal to this String with a given character replaced by another character. If this does not contain the character that is to be replaced, this is returned without modification.

ensure

   (   (result.length() == length())
    && (for all i=0..length()-1: result.charAt(i) == (charAt(i)==oldChar ? newChar: charAt(i))))
 

Parameters:
oldChar - the character to be replaced
newChar - the character oldChar is to be replaced with
Returns:
a String equal to this with oldChar replaced by newChar

matches

public boolean matches(String regex)
Test if this String matches the given regular expression.

This method behaves exactly as Pattern.matches(regex,this).

Parameters:
regex - regular expression to match.
Returns:
true if this String matches the regular expression.
Throws:
PatternSyntaxException - if the syntax of regex is invalid.
Since:
1.4

replaceFirst

public String replaceFirst(String regex,
                           String replacement)
Replaces the first substring that matches the given reugular expression.

This method behaves exactly as

   Pattern.compile(regex).matcher(this).replaceFirst(replacement)
 

Parameters:
regex - regular expression to match.
replacement - replacement string for the matched substring.
Returns:
resulting String with the replaced substring.
Throws:
PatternSyntaxException - if the syntax of regex is invalid.
Since:
1.4

replaceAll

public String replaceAll(String regex,
                         String replacement)
Replaces all occurrences of the substring that matches the given regular expression.

This method behaves exactly as

   Pattern.compile(regex).matcher(this).replaceAll(replacement)
 

Parameters:
regex - regular expression to match.
replacement - replacement string for the matched substring.
Returns:
resulting String with all replacements.
Throws:
PatternSyntaxException - if the syntax of regex is invalid.
Since:
1.4

split

public String[] split(String regex,
                      int limit)
split splits the string at substrings that match the given regular expression. If no substring matches the given regular expression then this string is returned as the only component in the returned array.

The limit parameter controls the spliting process:

  limit > 0  the array will have max limit entries and the last entry will contain the
             left over string after regex was applied (limit-1) times.
  limit == 0 regex will be applied as often as possible and trailing empty strings are
             discarted.
  limit < 0  regex will be applied as often as possible and trailing empty strings are
             *not* discarted.
 

This method behaves exactly as

   Pattern.compile(regex).split(this,limit);
 

Parameters:
regex - regular expression of the delimter substring.
limit - index for the split operation.
Returns:
array of Strings containing the string components.
Throws:
PatternSyntaxException - if the syntax of regex is invalid.
Since:
1.4

split

public String[] split(String regex)
split splits the string at substrings that match the given regular expression. If no substring matches the given regular expression then this string is returned as the only component in the returned array.

This method behaves exactly as a call to split(regex,0).

Parameters:
regex - regular expression of the delimter substring.
Returns:
array of Strings containing the string components.
Throws:
PatternSyntaxException - if the syntax of regex is invalid.
Since:
1.4

toLowerCase

public String toLowerCase(Locale locale)
toLowerCase converts this String to lower case respecting the given locale.

Parameters:
locale - the locale
Throws:
NullPointerException - if locale is null
Since:
1.1

toLowerCase

public String toLowerCase()
toLowerCase converts this String to lower case using the default locale.

This is equivalent to toLowerCase(Locale.getDefault()).

Since:
1.1

toUpperCase

public String toUpperCase(Locale locale)
toLowerCase converts this String to upper case respecting the given locale.

Parameters:
locale - the locale
Throws:
NullPointerException - if locale is null
Since:
1.1

toUpperCase

public String toUpperCase()
toUpperCase converts this String to lower case using the default locale.

This is equivalent to toUpperCase(Locale.getDefault()).

Since:
1.1

trim

public String trim()
trim removes all leading and trailing characters with a code <= ' ' from this String.

Returns:
a String with leading and trailing control and white space characters removed, this is there are no such characters.

toString

public String toString()
toString creates a printable string that represents this object for debugging purposes.

Specified by:
toString in interface CharSequence
Overrides:
toString in class Object
Returns:
this

toCharArray

public char[] toCharArray()
toCharArray allocates a new character array and copies the characters of this String into the array.

ensure

   (   result.length == length()
    && (for all i=0..length()-1: result[i] == charAt(i)))
 

Returns:
the new character array.

valueOf

public static String valueOf(Object obj)
valueOf returns a String created from a given object or null.

Parameters:
obj - an object or null
Returns:
(obj == null) ? "null" : obj.toString()

valueOf

public static String valueOf(char[] data)
valueOf returns a String containing the characters of the given character array.

ensure

   (   result.length() == data.length
    && for i in 0..data.length-1: result.charAt(i) == data[i])
 

Parameters:
data - the character array
Returns:
a new String containing the characters from data
Throws:
NullPointerException - if data is null

copyValueOf

public static String copyValueOf(char[] data)
copyValueOf returns a String containing the characters of the given character array.

ensure

   (   result.length() == data.length
    && for i in 0..data.length-1: result.charAt(i) == data[i])
 

This is equivalent to valueOf(data)

Parameters:
data - the character array
Returns:
a new String containing the characters from data
Throws:
NullPointerException - if data is null

valueOf

public static String valueOf(char[] data,
                             int offset,
                             int count)
valueOf creates a String that contains a subrange of the characters of a given character array.

ensure

   (   result.length() == count
    && for i in 0..count-1: result.charAt(i) == data[offset+i])
 

Parameters:
data - the character array
offset - the index of the first character to use in the string
count - the number of characters to use in the string.
Throws:
NullPointerException - if data is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>data.length.

copyValueOf

public static String copyValueOf(char[] data,
                                 int offset,
                                 int count)
copyValueOf creates a String that contains a subrange of the characters of a given character array.

ensure

   (   result.length() == count
    && for i in 0..count-1: result.charAt(i) == data[offset+i])
 

This is equivalent to valueOf(data,offset,count)

Parameters:
data - the character array
offset - the index of the first character to use in the string
count - the number of characters to use in the string.
Throws:
NullPointerException - if data is null.
IndexOutOfBoundsException - if count<0 or offset<0 or offset+count>data.length.

valueOf

public static String valueOf(boolean b)
valueOf converts a boolean to a String.

Parameters:
b - the boolean value
Returns:
b ? "true" : "false"

valueOf

public static String valueOf(char c)
valueOf creates a one-character string containing the given character.

A cache is used to avoid redundant allocations of the same string.

ensure

   (   result.length() == 1
    && result.charAt(0) == c);
 

Parameters:
c - the character
Returns:
a String of length 1 containing the character c.

valueOf

public static String valueOf(int i)
valueOf converts an int to a String.

Parameters:
i - an integer
Returns:
Integer.toString(i)

valueOf

public static String valueOf(long l)
valueOf converts a long to a String

Parameters:
l - a long
Returns:
Long.toString(l)

valueOf

public static String valueOf(float f)
valueOf converts a float to a String

Parameters:
f - a float
Returns:
Float.toString(f)

valueOf

public static String valueOf(double d)
valueOf converts a double to a String

Parameters:
d - a double
Returns:
Double.toString(d)

intern

public String intern()
intern interns this String. For any two Strings s1 and s2, the following holds:

   s1.equals(s2) <==> s1.intern() == s2.intern()
 

Note that String constants (such as "Hello") are automatically interned by the virtual machine, such that the following holds:

   s.equals("const string") <==> s.intern() == "const string"
 

intern() may be used for more efficient String equality comparison and to safe memory if many equal Strings are in use simulteneously.

However, intern() requires additional memory for each intern()ed String, such that this function should not be used without reason.

intern()ed Strings that become unused by the application will be reclaimed using weak references when the system memory runs low.

For JamaicaVM: intern() needs to allocate memory on the heap. If this function is called from within RTSJ's scoped memory (ScopedMemory), this method will anyway perform an allocation on the heap for the resulting intern()ed String. The resulting String will have its MemoryArea set to ImmortalMemory such that intern()ed Strings can be used even by code that cannot access heap objects (such as NoHeapRealtimeThread). However, the memory for intern()ed strings will be reclaimed after the last reference to such a string is deleted.

Returns:
a unique String for which result.equals(this) and MemoryArea.getMemoryArea(result)==ImmortalMemory.instance().

format

public static String format(String format,
                            Object... args)
Returns a formatted string using the specified format string and arguments.

The locale always used is the one returned by Locale.getDefault().

Parameters:
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification.
NullPointerException - If the format is null
Since:
1.5
See Also:
Formatter

format

public static String format(Locale l,
                            String format,
                            Object... args)
Returns a formatted string using the specified locale, format string, and arguments.

Parameters:
l - The locale to apply during formatting. If l is null then no localization is applied.
format - A format string
args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the Details section of the formatter class specification
NullPointerException - If the format is null
Since:
1.5
See Also:
Formatter

isEmpty

public boolean isEmpty()
Returns true if, and only if, length() is 0.

Returns:
true if length() is 0, otherwise false
Since:
1.6

aicas logoJamaica 6.4 release 1

aicas GmbH, Karlsruhe, Germany —www.aicas.com
Copyright © 2001-2015 aicas GmbH. All Rights Reserved.