How do I remove leading and trailing white space from string?

package org.kodejava.lang;

public class TrimStringExample {
    public static void main(String[] args) {
        String text = "     tattarrattat     ";
        System.out.println("Original      = " + text);
        System.out.println("text.length() = " + text.length());

        // The trim() method will result a new string object with a leading and
        // trailing while space removed.
        text = text.trim();
        System.out.println("Result        = " + text);
        System.out.println("text.length() = " + text.length());
    }
}

The result of the code snippet above:

Original      =      tattarrattat     
text.length() = 22
Result        = tattarrattat
text.length() = 12

How do I add leading zeros to a number?

This example shows you how to use the String.format() method to add zero padding to a number. If you just want to print out the result you can use System.out.format(). This method is available since Java 1.5, so If you use a previous version you can use the NumberFormat class, see: How do I format a number with leading zeros?.

package org.kodejava.lang;

public class LeadingZerosExample {
    public static void main(String[] args) {
        int number = 1500;

        // String format below will add leading zeros (the %0 syntax)
        // to the number above. The length of the formatted string will
        // be 7 characters.
        String formatted = String.format("%07d", number);

        System.out.println("Number with leading zeros: " + formatted);
    }
}

Here is the result of the code snippet above:

Number with leading zeros: 0001500

For more information about the format syntax you can find it here.

How do I convert string to an integer or number?

package org.kodejava.lang;

public class StringToInteger {
    public static void main(String[] args) {
        // Some random selected number, could representing a decimal,
        // hexadecimal or octal number.
        String myLuckyNumber = "13";

        // We convert a string to an integer by invoking parseInt() method
        // of the Integer class.
        int number = Integer.parseInt(myLuckyNumber);
        System.out.println("My lucky number is: " + number);

        // We can also converting a string representation of a number other
        // then the decimal base, for instance an hexadecimal by providing
        // the radix to the method.
        number = Integer.parseInt(myLuckyNumber, 16);
        System.out.println("My lucky number is: " + number);

        number = Integer.parseInt(myLuckyNumber, 8);
        System.out.println("My lucky number is: " + number);
    }
}

Our code results are:

My lucky number is: 13
My lucky number is: 19
My lucky number is: 11

How do I reverse a string?

Below is an example code that reverse a string. In this example we use StringBuffer.reverse() method to reverse a string. In Java 1.5, a new class called StringBuilder also has a reverse() method that do just the same, one of the difference is StringBuffer class is synchronized while StringBuilder class is not.

And here is the string reverse in the StringBuffer way.

package org.kodejava.lang;

public class StringReverseExample {
    public static void main(String[] args) {
        // The normal sentence that is going to be reversed.
        String words =
                "Morning of The World - The Last Paradise on Earth";

        // To reverse the string we can use the reverse() method in
        // the StringBuffer class. The reverse() method returns a
        // StringBuffer so we need to call the toString() method to
        // get a string object.
        String reverse = new StringBuffer(words).reverse().toString();

        // Print the normal string
        System.out.println("Normal : " + words);
        // Print the string in reversed order
        System.out.println("Reverse: " + reverse);
    }
}

Beside using this simple method you can try to reverse a string by converting it to character array and then reverse the array order.

And below is the result of the code snippet above.

Normal : Morning of The World - The Last Paradise on Earth
Reverse: htraE no esidaraP tsaL ehT - dlroW ehT fo gninroM

How do I convert string to char array?

Here we have a small class that convert a string literal into an array, a character array. To do this we can simply use String.toCharArray() method.

package org.kodejava.lang;

public class StringToArrayExample {
    public static void main(String[] args) {
        // We have a string literal that contains the tag line of this blog.
        String literal = "Kode Java - Learn Java by Examples";

        // Now we want to convert or divided it into a small array of char.
        // To do this we can simply used String.toCharArray() method. This
        // method splits the string into an array of characters.
        char[] temp = literal.toCharArray();

        // Here we just iterate the char array and print it to our console.
        for (char c : temp) {
            System.out.print(c);
        }
    }
}