1) String objects are immutable(read-only), and you have a StringBuilder class for mutable strings. More generally, the java.lang.CharSequence interface is implemented by any class that represents a character sequence, including String, StringBuilder and StringBuffer. The java.noi.CharBuffer class is used for performing I/O.
2) String Constructors:
public String()
Constructs a new String with the value ""an empty string.
public String(String value)
Constructs a new String that is a copy of the specified String object valuethis is a copy constructor. Because String objects are immutable, this is rarely used.
public String(StringBuilder value)
Constructs a new String with the same contents as the given StringBuilder.
public String(StringBuffer value)
Constructs a new String with the same contents as the given StringBuffer.
Find the first or last occurrence of a particular character or substring in a string:
String comprasions:
Characters in strings are compared numerically by their Unicode values, not by their localized notion of order.
equals test for whether the content is identical, equalsIgnoreCase ignore the case of the character;
String implements the interface Comparable<String>, so it has compareTo method returns an int that is less than, equal to, or greater than zero when the string on which it is invoked is less than, equal to, or greater than the other string. compareToIgnoreCase is the corresponding “Ignore Case” version.
public boolean regionMatches(int start, String other, int ostart, int count)
Returns true if the given region of this String has the same Unicode characters as the given region of the string other. Checking starts in this string at the position start, and in the other string at position ostart. Only the first count characters are compared.
public boolean regionMatches(boolean ignoreCase, int start, String other, int ostart, int count)
This version of regionMatches behaves exactly like the previous one, but the boolean ignoreCase controls whether case is significant.
String Literals:
Using operator == on objects only tests whether the two references refer to the same object, not whether they are equivalent objects. However, any two string literals with the same contents will refer to the same String object. USE equals when you want to test whether two String objects have the same content.
Example:
public static void main(String[] args) { String str1 = "JavaBeta"; if(str1 == "JavaBeta") System.out.println("Equal"); else System.out.println("Unequal"); String str2 = "Java" + "Beta"; if(str2 == "JavaBeta") System.out.println("Equal"); else System.out.println("Unequal"); String str3 = "Java"; str3 += "Beta"; if(str3 == "JavaBeta") System.out.println("Equal"); else System.out.println("Unequal"); }
Output:
Equal
Equal
Unequal
The intern method returns a String that has the same contents as the one it is invoked on. However, any two strings with the same contents return the same String object from intern.
Example:
public static void main(String[] args) { String str1 = "Java"; String str2 = "Java"; str1 += "Beta"; str2 += "Beta"; if(str1 == str2) System.out.println("Equal"); else System.out.println("Unequal"); if(str1.equals(str2)) System.out.println("Equal"); else System.out.println("Unequal"); if(str1.intern() == str2.intern()) System.out.println("Equal"); else System.out.println("Unequal"); }
Output:
Unequal
Equal
Equal
String Replacement:
public String replace(char oldChar, char newChar)
Returns a String with all instances of oldChar replaced with the character newChar.
public String replace(CharSequence oldSeq, CharSquence newSeq)
Returns a String with each occurrence of the subsequence oldSeq replaced by the subsequence newSeq.
public String trim()
Returns a String with leading and trailing whitespace stripped(the internal whitespace is unchanged). Whitespace characters are those identified as such by the Character.isWhitespace method and include space, tab, and newline.
String Conversion:
For numeric types, if the string does not represent a valid value of that type, a NumberFormatException is thrown.
Strings and char Arrays:
There are two constructors for creating a String from a char array:
public String(char[] chars, int start, int count)
Constructs a new String whose contents are the same as the chars array, from index start up to a maximum of count characters.
public String(char[] chars)
Equivalent to String(chars,0, chars.length).
Converts this string to a new character array.
public char[] toCharArray()
- A newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this String into the specified array. The characters of the specified substring are copied into the character array, starting at dst[dstBegin]. The specified substring is the part of the string starting at srcBegin, up to but not including srcEnd.
Character Set Encoding:
Character sets and their encoding mechanisms are represented by specific classes within the java.nio.charset package:
Charset
A named mapping (such as US-ASCII or UTF-8) between sequences of 16-bit Unicode code units and sequences of bytes. This contains general information on the sequence encoding, simple mechanisms for encoding and decoding, and methods to create CharsetEncoder and CharsetDecoder objects for richer abilities.
CharsetEncoder
An object that can transform a sequence of 16-bit Unicode code units into a sequence of bytes in a specific character set. The encoder object also has methods to describe the encoding.
CharsetDecoder
An object that can transform a sequence of bytes in a specific character set into a sequence of 16-bit Unicode code units. The decoder object also has methods to describe the decoding.
You can obtain a Charset via its own static forName method, though usually you will just specify the character set name to some other method (such as the String constructor or an I/O operation) rather than working with the Charset object directly. To test whether a given character set is supported use the forName method, and if you get an UnsuppportedCharsetException then it is not.
You can find a list of available character sets from the static availableCharsets method, which returns a SortedMap of names and Charset instances, of all known character sets. For example, to print out the names of all the known character sets you can use:
for (String name : Charset.availableCharsets().keySet()) System.out.println(name);
Every instance of the Java virtual machine has a default character set that is determined during virtual-machine startup and typically depends on the locale and encoding being used by the underlying operating system. You can obtain the default Charset using the static defaultCharset method.
3) To build and modify a string, you probably want to use the StringBuilder class. StringBuilder provides the following constructors:
public StringBuilder()
Constructs a StringBuilder with an initial value of "" (an empty string) and a capacity of 16.
public StringBuilder(int capacity)
Constructs a StringBuilder with an initial value of "" and the given capacity.
public StringBuilder(String str)
Constructs a StringBuilder with an initial value copied from str.
public StringBuilder(CharSequence seq)
Constructs a StringBuilder with an initial value copied from seq.
StringBuilder is similar to String, and it supports many methods that have the same names and contracts as some String methodsindexOf, lastIndexof, replace, substring. However, StringBuilder does not extend String nor vice versa. They are independent implementations of CharSequence.
public Appendable append(char c) public Appendable append(CharSequence seq) public Appendable append(CharSequence seq, int start, int end)
public StringBuilder insert(int index, char[] str, int offset, int len)
Inserts the string representation of a subarray of the str
array argument into this sequence. The subarray begins at the specified offset
and extends len
char
s. The characters of the subarray are inserted into this sequence at the position indicated by index
. The length of this sequence increases by len
char
s.
- Parameters:
index
- position at which to insert subarray.str
- Achar
array.offset
- the index of the firstchar
in subarray to be inserted.len
- the number ofchar
s in the subarray to be inserted.- Returns:
- This object
- Throws:
StringIndexOutOfBoundsException
- ifindex
is negative or greater thanlength()
, oroffset
orlen
are negative, or(offset+len)
is greater thanstr.length
.
public StringBuilder replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String
. The substring begins at the specified start
and extends to the character at index end - 1
or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String
is inserted at start
. (This sequence will be lengthened to accommodate the specified String if necessary.)
- Parameters:
start
- The beginning index, inclusive.end
- The ending index, exclusive.str
- String that will replace previous contents.- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
public StringBuilder delete(int start, int end)
Removes the characters in a substring of this sequence. The substring begins at the specified start
and extends to the character at index end - 1
or to the end of the sequence if no such character exists. Ifstart
is equal to end
, no changes are made.
- Parameters:
start
- The beginning index, inclusive.end
- The ending index, exclusive.- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException
- ifstart
is negative, greater thanlength()
, or greater thanend
.
public StringBuilder deleteCharAt(int index)
Removes the char
at the specified position in this sequence. This sequence is shortened by one char
.
Note: If the character at the given index is a supplementary character, this method does not remove the entire character. If correct handling of supplementary characters is required, determine the number ofchar
s to remove by calling Character.charCount(thisSequence.codePointAt(index))
, wherethisSequence
is this sequence.
- Parameters:
index
- Index ofchar
to remove- Returns:
- This object.
- Throws:
StringIndexOutOfBoundsException
- if theindex
is negative or greater than or equal tolength()
.
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character array dst
. The first character to be copied is at index srcBegin
; the last character to be copied is at index srcEnd-1
. 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
- start copying at this offset.srcEnd
- stop copying at this offset.dst
- the array to copy the data into.dstBegin
- offset intodst
.- Throws:
NullPointerException
- ifdst
isnull
.IndexOutOfBoundsException
- if any of the following is true:srcBegin
is negativedstBegin
is negative- the
srcBegin
argument is greater than thesrcEnd
argument. srcEnd
is greater thanthis.length()
.dstBegin+srcEnd-srcBegin
is greater thandst.length
public StringBuilder(int capacity)
Constructs a StringBuilder with the given initial capacity and an initial value of "".
public void ensureCapacity(int minimum)
Ensures that the capacity of the buffer is at least the specified minimum.
public int capacity()
Returns the current capacity of the buffer.
public void trimToSize()
Attempts to reduce the capacity of the buffer to accommodate the current sequence of characters. There is no guarantee that this will actually reduce the capacity of the buffer, but this gives a hint to the system that it may be a good time to try and reclaim some storage space.