Fundamentals (Docs)



Whenever it encounters a string literal in your code, the compiler creates a String object with its value.
	String greeting = "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); // hello.

String Length

Methods used to obtain information about an object are known as accessor methods.

	String palindrome = "Dot saw I was Tod";
	int len = palindrome.length();

String Chars
	char[] charArray = new char[len];
        char[] tmpCharArray = new char[len];
	// copies characters from this string into the destination character array
	palindrome.getChars(0, len, tmpCharArray, 0);
        // reverse
        for (int i = 0; i < len; i++) {
            charArray[i] = tmpCharArray[len - 1 - i];
        String reversePalindrome = new String(charArray);
		// output: doT saw I was toD

String Concatenation

The String class includes a method for concatenating two strings:

	string1.concat(string2); // or
	"My name is ".concat("Rumplestiltskin"); // or
	"Hello," + " world" + "!" // most common

The Java programming language does not permit literal strings to span lines in source files, 
so you must use the + 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.";

String Format

You have seen the use of the 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.

	String fs = String.format("The value of the float variable is %.2f", 11.222);
        System.out.println(fs); // The value of the float variable is 11.22

Converting Strings to Numbers

Frequently, a program ends up with numeric data in a string object - a value entered by the user, for example.

The Number subclasses that wrap primitive numeric types provide a class method named valueOf 
that converts a string to an object of that type.

	String a1 = "1";
        String a2 = "1.11";
        Integer b1 = Integer.valueOf(a1);
        Float b2 = Float.valueOf(a2);
        System.out.println(b1 + b2); // 2.1100001

Converting Numbers to Strings

	int i;
	String s1 = "" + i; // or
	String s2 = String.valueOf(i); // or
	String s3 = Integer.toString(i);

Getting Characters and Substrings by Index

	String anotherPalindrome = "Niagara. O roar again!"; 
	char aChar = anotherPalindrome.charAt(9); // O
	String roar = anotherPalindrome.substring(11, 15); // roar

IndexOf Example

	public class FilenameDemo {
	    public static void main(String[] args) {
		final String FPATH = "/home/my/index.html";
		Filename myHomePage = new Filename(FPATH, '/', '.');

		System.out.println("Extension: " + myHomePage.getExtension()); // html
		System.out.println("Filename: " + myHomePage.getFilename()); // index
		System.out.println("Path: " + myHomePage.getPath()); // /home/my

	class Filename {
	    private String fpath;
	    private char pathSeparator;
	    private char extensionSeparator;

	    public Filename(String fpath, char pathSeparator, char extensionSeparator) {
		this.fpath = fpath;
		this.pathSeparator = pathSeparator;
		this.extensionSeparator = extensionSeparator;

	    public String getExtension() {
		return fpath.substring(fpath.lastIndexOf(extensionSeparator) + 1);
	    public String getFilename() {
		return fpath.substring(fpath.lastIndexOf(pathSeparator) + 1, fpath.lastIndexOf(extensionSeparator));
	    public String getPath() {
		return fpath.substring(0, fpath.lastIndexOf(pathSeparator));