Intel

Developer Zone

String

java.lang

Class String



  • public final class String
    extends Object
    An immutable sequence of characters/code units (chars). A String is represented by array of UTF-16 values, such that Unicode supplementary characters (code points) are stored/encoded as surrogate pairs via Unicode code units (char).
    Since:
    1.0
    See Also:
    StringBuffer, StringBuilder, Charset
    • Constructor Summary

      Constructors
      Constructor and Description
      String()
      Creates an empty string.
      String(byte[] data)
      Converts the byte array to a string using the default encoding as specified by the file.encoding system property.
      String(byte[] data, int start, int length)
      Converts the byte array to a string using the default encoding as specified by the file.encoding system property.
      String(byte[] data, int start, int length, String encoding)
      Converts the byte array to a string using the specified encoding.
      String(byte[] data, String encoding)
      Converts the byte array to a string using the specified encoding.
      String(char[] data)
      Initializes this string to contain the characters in the specified character array.
      String(char[] data, int start, int length)
      Initializes this string to contain the specified characters in the character array.
      String(String string)
      Creates a String that is a copy of the specified string.
      String(StringBuffer stringbuffer)
      Creates a String from the contents of the specified StringBuffer.
    • Method Summary

      Methods
      Modifier and Type Method and Description
      char charAt(int index)
      Returns the character at the specified offset in this string.
      int compareTo(String string)
      Compares the specified string to this string using the Unicode values of the characters.
      String concat(String string)
      Concatenates this string and the specified string.
      boolean endsWith(String suffix)
      Compares the specified string to this string to determine if the specified string is a suffix.
      boolean equals(Object object)
      Compares the specified object to this string and returns true if they are equal.
      byte[] getBytes()
      Converts this string to a byte array using the default encoding as specified by the file.encoding system property.
      void getBytes(int start, int end, byte[] data, int index)
      Converts this string to a byte array, ignoring the high order bits of each character.
      byte[] getBytes(String encoding)
      Converts this string to a byte array using the specified encoding.
      void getChars(int start, int end, char[] buffer, int index)
      Copies the specified characters in this string to the character array starting at the specified offset in the character array.
      int hashCode()
      Returns an integer hash code for this int.
      int indexOf(int c)
      Searches in this string for the first index of the specified character.
      int indexOf(int c, int start)
      Searches in this string for the index of the specified character.
      int indexOf(String string)
      Searches in this string for the first index of the specified string.
      int indexOf(String subString, int start)
      Searches in this string for the index of the specified string.
      int lastIndexOf(int c)
      Searches in this string for the last index of the specified character.
      int lastIndexOf(int c, int start)
      Searches in this string for the index of the specified character.
      int length()
      Returns the size of this string.
      boolean regionMatches(boolean ignoreCase, int thisStart, String string, int start, int length)
      Compares the specified string to this string and compares the specified range of characters to determine if they are the same.
      String replace(char oldChar, char newChar)
      Copies this string replacing occurrences of the specified character with another character.
      boolean startsWith(String prefix)
      Compares the specified string to this string to determine if the specified string is a prefix.
      boolean startsWith(String prefix, int start)
      Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.
      String substring(int start)
      Copies a range of characters into a new string.
      String substring(int start, int end)
      Copies a range of characters into a new string.
      char[] toCharArray()
      Copies the characters in this string to a character array.
      String toLowerCase()
      Converts the characters in this string to lowercase, using the default Locale.
      String toString()
      Returns this string.
      String toUpperCase()
      Converts the characters in this string to uppercase, using the default Locale.
      String trim()
      Copies this string removing white space characters from the beginning and end of the string.
      static String valueOf(boolean value)
      Converts the specified boolean to its string representation.
      static String valueOf(char value)
      Converts the specified character to its string representation.
      static String valueOf(char[] data)
      Creates a new string containing the characters in the specified character array.
      static String valueOf(char[] data, int start, int length)
      Creates a new string containing the specified characters in the character array.
      static String valueOf(int value)
      Converts the specified integer to its string representation.
      static String valueOf(long l)
      static String valueOf(Object value)
      Converts the specified object to its string representation.
    • Constructor Detail

      • String

        public String()
        Creates an empty string.
      • String

        public String(String string)
        Creates a String that is a copy of the specified string.
        Parameters:
        string - the string to copy.
      • String

        public String(char[] data)
        Initializes this string to contain the characters in the specified character array. Modifying the character array after creating the string has no effect on the string.
        Parameters:
        data - the array of characters.
        Throws:
        NullPointerException - when data is null.
      • String

        public String(char[] data,
              int start,
              int length)
        Initializes this string to contain the specified characters in the character array. Modifying the character array after creating the string has no effect on the string.
        Parameters:
        data - the array of characters.
        start - the starting offset in the character array.
        length - the number of characters to use.
        Throws:
        NullPointerException - when data is null.
        IndexOutOfBoundsException - if length < 0, start < 0 or start + length > data.length
      • String

        public String(StringBuffer stringbuffer)
        Creates a String from the contents of the specified StringBuffer.
        Parameters:
        stringbuffer - the buffer to get the contents from.
      • String

        public String(byte[] data)
        Converts the byte array to a string using the default encoding as specified by the file.encoding system property. If the system property is not defined, the default encoding is ISO8859_1 (ISO-Latin-1). If 8859-1 is not available, an ASCII encoding is used.
        Parameters:
        data - the byte array to convert to a string.
      • String

        public String(byte[] data,
              int start,
              int length)
        Converts the byte array to a string using the default encoding as specified by the file.encoding system property. If the system property is not defined, the default encoding is UTF-8. UTF-8 is not available, an ASCII encoding is used.
        Parameters:
        data - the byte array to convert to a string.
        start - the starting offset in the byte array.
        length - the number of bytes to convert.
        Throws:
        NullPointerException - when data is null.
        IndexOutOfBoundsException - if length < 0, start < 0 or start + length > data.length.
      • String

        public String(byte[] data,
              int start,
              int length,
              String encoding)
               throws UnsupportedEncodingException
        Converts the byte array to a string using the specified encoding.
        Parameters:
        data - the byte array to convert to a string.
        start - the starting offset in the byte array.
        length - the number of bytes to convert.
        encoding - the encoding.
        Throws:
        NullPointerException - when data is null.
        IndexOutOfBoundsException - if length < 0, start < 0 or start + length > data.length.
        UnsupportedEncodingException - if encoding is not supported.
    • Method Detail

      • charAt

        public char charAt(int index)
        Returns the character at the specified offset in this string.
        Parameters:
        index - the zero-based index in this string.
        Returns:
        the character at the index.
        Throws:
        IndexOutOfBoundsException - if index < 0 or index >= length().
      • compareTo

        public int compareTo(String string)
        Compares the specified string to this string using the Unicode values of the characters. Returns 0 if the strings contain the same characters in the same order. Returns a negative integer if the first non-equal character in this string has a Unicode value which is less than the Unicode value of the character at the same position in the specified string, or if this string is a prefix of the specified string. Returns a positive integer if the first non-equal character in this string has a Unicode value which is greater than the Unicode value of the character at the same position in the specified string, or if the specified string is a prefix of this string.
        Parameters:
        string - the string to compare.
        Returns:
        0 if the strings are equal, a negative integer if this string is before the specified string, or a positive integer if this string is after the specified string.
        Throws:
        NullPointerException - if string is null.
      • concat

        public String concat(String string)
        Concatenates this string and the specified string.
        Parameters:
        string - the string to concatenate
        Returns:
        a new string which is the concatenation of this string and the specified string.
      • endsWith

        public boolean endsWith(String suffix)
        Compares the specified string to this string to determine if the specified string is a suffix.
        Parameters:
        suffix - the suffix to look for.
        Returns:
        true if the specified string is a suffix of this string, false otherwise.
        Throws:
        NullPointerException - if suffix is null.
      • equals

        public boolean equals(Object object)
        Compares the specified object to this string and returns true if they are equal. The object must be an instance of string with the same characters in the same order.
        Overrides:
        equals in class Object
        Parameters:
        object - the object to compare.
        Returns:
        true if the specified object is equal to this string, false otherwise.
        See Also:
        hashCode
      • getBytes

        public byte[] getBytes()
        Converts this string to a byte array using the default encoding as specified by the file.encoding system property. If the system property is not defined, the default encoding is UTF-8. If UTF-8 is not available, an ASCII encoding is used.
        Returns:
        the byte array encoding of this string.
      • getBytes

        public void getBytes(int start,
                    int end,
                    byte[] data,
                    int index)
        Converts this string to a byte array, ignoring the high order bits of each character.
        Parameters:
        start - the starting offset of characters to copy.
        end - the ending offset of characters to copy.
        data - the destination byte array.
        index - the starting offset in the destination byte array.
        Throws:
        NullPointerException - if data is null.
        IndexOutOfBoundsException - if start < 0, end > length(), index < 0 or end - start > data.length - index.
      • getBytes

        public byte[] getBytes(String encoding)
                        throws UnsupportedEncodingException
        Converts this string to a byte array using the specified encoding.
        Parameters:
        encoding - the encoding to use.
        Returns:
        the encoded byte array of this string.
        Throws:
        UnsupportedEncodingException - if the encoding is not supported.
      • getChars

        public void getChars(int start,
                    int end,
                    char[] buffer,
                    int index)
        Copies the specified characters in this string to the character array starting at the specified offset in the character array.
        Parameters:
        start - the starting offset of characters to copy.
        end - the ending offset of characters to copy.
        buffer - the destination character array.
        index - the starting offset in the character array.
        Throws:
        NullPointerException - if buffer is null.
        IndexOutOfBoundsException - if start < 0, end > length(), start > end, index < 0, end - start > buffer.length - index
      • indexOf

        public int indexOf(int c,
                  int start)
        Searches in this string for the index of the specified character. The search for the character starts at the specified offset and moves towards the end of this string.
        Parameters:
        c - the character to find.
        start - the starting offset.
        Returns:
        the index in this string of the specified character, -1 if the character isn't found.
      • indexOf

        public int indexOf(int c)
        Searches in this string for the first index of the specified character. The search for the character starts at the beginning and moves towards the end of this string.
        Parameters:
        c - the character to find.
        Returns:
        the index in this string of the specified character, -1 if the character isn't found.
      • indexOf

        public int indexOf(String subString,
                  int start)
        Searches in this string for the index of the specified string. The search for the string starts at the specified offset and moves towards the end of this string.
        Parameters:
        subString - the string to find.
        start - the starting offset.
        Returns:
        the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if subString is null.
      • indexOf

        public int indexOf(String string)
        Searches in this string for the first index of the specified string. The search for the string starts at the beginning and moves towards the end of this string.
        Parameters:
        string - the string to find.
        Returns:
        the index of the first character of the specified string in this string, -1 if the specified string is not a substring.
        Throws:
        NullPointerException - if string is null.
      • lastIndexOf

        public int lastIndexOf(int c,
                      int start)
        Searches in this string for the index of the specified character. The search for the character starts at the specified offset and moves towards the beginning of this string.
        Parameters:
        c - the character to find.
        start - the starting offset.
        Returns:
        the index in this string of the specified character, -1 if the character isn't found.
      • lastIndexOf

        public int lastIndexOf(int c)
        Searches in this string for the last index of the specified character. The search for the character starts at the end and moves towards the beginning of this string.
        Parameters:
        c - the character to find.
        Returns:
        the index in this string of the specified character, -1 if the character isn't found.
      • length

        public int length()
        Returns the size of this string.
        Returns:
        the number of characters in this string.
      • regionMatches

        public boolean regionMatches(boolean ignoreCase,
                            int thisStart,
                            String string,
                            int start,
                            int length)
        Compares the specified string to this string and compares the specified range of characters to determine if they are the same. When ignoreCase is true, the case of the characters is ignored during the comparison.
        Parameters:
        ignoreCase - specifies if case should be ignored.
        thisStart - the starting offset in this string.
        string - the string to compare.
        start - the starting offset in the specified string.
        length - the number of characters to compare.
        Returns:
        true if the ranges of characters are equal, false otherwise.
        Throws:
        NullPointerException - if string is null.
      • replace

        public String replace(char oldChar,
                     char newChar)
        Copies this string replacing occurrences of the specified character with another character.
        Parameters:
        oldChar - the character to replace.
        newChar - the replacement character.
        Returns:
        a new string with occurrences of oldChar replaced by newChar.
      • startsWith

        public boolean startsWith(String prefix,
                         int start)
        Compares the specified string to this string, starting at the specified offset, to determine if the specified string is a prefix.
        Parameters:
        prefix - the string to look for.
        start - the starting offset.
        Returns:
        true if the specified string occurs in this string at the specified offset, false otherwise.
        Throws:
        NullPointerException - if prefix is null.
      • startsWith

        public boolean startsWith(String prefix)
        Compares the specified string to this string to determine if the specified string is a prefix.
        Parameters:
        prefix - the string to look for.
        Returns:
        true if the specified string is a prefix of this string, false otherwise
        Throws:
        NullPointerException - if prefix is null.
      • substring

        public String substring(int start)
        Copies a range of characters into a new string.
        Parameters:
        start - the offset of the first character.
        Returns:
        a new string containing the characters from start to the end of the string.
        Throws:
        IndexOutOfBoundsException - if start < 0 or start > length().
      • substring

        public String substring(int start,
                       int end)
        Copies a range of characters into a new string.
        Parameters:
        start - the offset of the first character.
        end - the offset one past the last character.
        Returns:
        a new string containing the characters from start to end - 1
        Throws:
        IndexOutOfBoundsException - if start < 0, start > end or end > length().
      • toCharArray

        public char[] toCharArray()
        Copies the characters in this string to a character array.
        Returns:
        a character array containing the characters of this string.
      • toLowerCase

        public String toLowerCase()
        Converts the characters in this string to lowercase, using the default Locale.
        Returns:
        a new string containing the lowercase characters equivalent to the characters in this string.
      • toUpperCase

        public String toUpperCase()
        Converts the characters in this string to uppercase, using the default Locale.
        Returns:
        a new string containing the uppercase characters equivalent to the characters in this string.
      • toString

        public String toString()
        Returns this string.
        Overrides:
        toString in class Object
        Returns:
        this string.
      • trim

        public String trim()
        Copies this string removing white space characters from the beginning and end of the string.
        Returns:
        a new string with characters removed from the beginning and the end.
      • valueOf

        public static String valueOf(char value)
        Converts the specified character to its string representation.
        Parameters:
        value - the character.
        Returns:
        the character converted to a string.
      • valueOf

        public static String valueOf(char[] data)
        Creates a new string containing the characters in the specified character array. Modifying the character array after creating the string has no effect on the string.
        Parameters:
        data - the array of characters.
        Returns:
        the new string.
        Throws:
        NullPointerException - if data is null.
      • valueOf

        public static String valueOf(char[] data,
                     int start,
                     int length)
        Creates a new string containing the specified characters in the character array. Modifying the character array after creating the string has no effect on the string.
        Parameters:
        data - the array of characters.
        start - the starting offset in the character array.
        length - the number of characters to use.
        Returns:
        the new string.
        Throws:
        IndexOutOfBoundsException - if length < 0, start < 0 or start + length > data.length
        NullPointerException - if data is null.
      • valueOf

        public static String valueOf(boolean value)
        Converts the specified boolean to its string representation. When the boolean is true return "true", otherwise return "false".
        Parameters:
        value - the boolean.
        Returns:
        the boolean converted to a string.
      • valueOf

        public static String valueOf(int value)
        Converts the specified integer to its string representation.
        Parameters:
        value - the integer.
        Returns:
        the integer converted to a string.
      • valueOf

        public static String valueOf(long l)
      • valueOf

        public static String valueOf(Object value)
        Converts the specified object to its string representation. If the object is null return the string "null", otherwise use toString() to get the string representation.
        Parameters:
        value - the object.
        Returns:
        the object converted to a string, or the string "null".