The Java platform provides the
String class to create and manipulate strings.
String greeting = "Hello world!";
String object with its value—in this case, Hello world!.
As with any other object, you can create String objects by using the
new keyword and a constructor.
The String class has thirteen constructors that allow
you to provide the initial value of the string using different sources,
such as an array of characters:
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
System.out.println(helloString);
hello.
String class is
immutable, so that once it is created a String object cannot be changed.
The String class has a number of methods, some of which will be discussed below,
that appear to modify strings. Since strings are immutable, what these methods
really do is create and return a new string that contains the result of the operation.
length() method,
which returns the number of characters contained in the string object.
After the following two lines of code have been executed, len equals 17:
String palindrome = "Dot saw I was Tod"; int len = palindrome.length();
String method charAt(i), which returns
the ith character in the string, counting from 0.
public class StringDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
doT saw I was toD
for loop),
reverse the array into a second array (second for loop), and then convert back to a string. The
String class includes a method, getChars(), to convert a string, or a portion of a string, into
an array of characters so we could replace the first for loop
in the program above with
palindrome.getChars(0, len, tempCharArray, 0);
String class includes a method for concatenating two strings:
string1.concat(string2);
You can also use the concat() method with string literals, as in:
"My name is ".concat("Rumplestiltskin");
+ operator, as in
"Hello," + " world" + "!"
"Hello, world!"
+ operator is widely used in print statements. For example:
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
Dot saw I was Tod
String,
its toString() method is called to convert it to a String.
+ concatenation operator at the end of each line in a
multi-line string. For
example,
String quote = "Now is the time for all good " + "men to come to the aid of their country.";
+ concatenation operator is, once
again, very common in print statements.
printf() and format() methods to print output with
formatted numbers. The String class has an equivalent class method, format(),
that returns a String object rather than a PrintStream object.
Using String's static format() method allows you to create a
formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of
System.out.printf("The value of the float variable is %f, while the value of the " +
"integer variable is %d, and the string is %s", floatVar, intVar, stringVar);
String fs;
fs = String.format("The value of the float variable is %f, while the value of the " +
"integer variable is %d, and the string is %s", floatVar, intVar, stringVar);
System.out.println(fs);