Friday, January 24, 2014

Convert between String and char array

Convert between String and char array
Convert between String and char array

package javastringtochararray;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaStringToCharArray {

    public static void main(String[] args) {
        String src = "Hello Java-buddy";
        char[] array;
        
        //convert String to char array
        array = src.toCharArray();
        for(int c = 0; c<array.length; c++){
            System.out.print(array[c]);
        }
        System.out.println();
        
        //convert char array to String
        String newString = new String(array);
        System.out.println(newString);
        
        System.out.println("src.equals(newString) = " + src.equals(newString));
    }
    
}

No comments:

Post a Comment