You are on page 1of 6

Convert String to Character Array Example

public class First { public static void main(String args[]) { String str = "Hello World"; char[] ch; ch = str.toCharArray(); for(int index=0; index < ch.length; index++) System.out.print(ch[index]); } }
=====================================

Java String Compare Example


Java String class defines following methods to compare Java String object. 1) int compareTo( String anotherString ) compare two string based upon the unicode value of each character in the String. Returns negative int if first string is less than another Returns positive int if first string is grater than another Returns 0 if both strings are same. 2) int compareTo( Object obj ) Behaves exactly like compareTo ( String anotherString) if the argument object is of type String, otherwise throws ClassCastException. 3) int compareToIgnoreCase( String anotherString ) Compares two strings ignoring the character case of the given String. public static void main(String args[]) { String str = "Hello World"; String anotherString = "hello world"; Object objStr = str; System.out.println( str.compareTo(anotherString) );

System.out.println( str.compareToIgnoreCase(anotherString) ); System.out.println( str.compareTo(objStr) ); } } ===================================

String toUpperCase example


public static void main(String[] args) { String str = "Hello Anuj"; String strUpper = str.toUpperCase(); System.out.println("Original String: " + str); System.out.println("String changed to upper case: " + strUpper); } } ===================================

Java String Length Example


public static void main(String[] args) { String str = "Hello World"; int length = str.length(); System.out.println("Length of a String is : " + length); } } ====================================

Java String Replace Example


Java String class defines three methods to replace character. 1) String replace(int oldChar, int newChar) This method replaces a specified character with new character and returns a new string object. 2) String replaceFirst(String regularExpression, String newString) Replaces the first substring of this string that matches the given regular expression with the given new string. 3) String replaceAll(String regex, String replacement) Replaces the each substring of this string that matches the given regular expression with the given new string.

public class First { public static void main(String args[]) { String str="Replace Region"; System.out.println( str.replace( 'R','A' ) ); System.out.println( str.replaceFirst("Re", "Ra") );//Replaces only first occourances of given String with new one System.out.println( str.replaceAll("Re", "Ra") ); } } ==============================

Java String Split Example


Java String class defines following methods to split Java String object. String[] split( String regularExpression ) Splits the string according to given regular expression. String[] split( String reularExpression, int limit ) Splits the string according to given regular expression. The number of resultant substrings by splitting the string is controlled by limit argument. IMPORTANT NOTE: Some special characters need to be escaped while providing them as delimiters like "." and "|". public static void main(String args[]) { String str = "one-two-three"; String[] temp; String delimiter = "-"; temp = str.split(delimiter); for(int i =0; i < temp.length ; i++) { System.out.println(temp[i]);

} System.out.println(""); str = "one.two.three"; delimiter = "\\."; temp = str.split(delimiter); for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); System.out.println(""); temp = str.split(delimiter,2); for(int i =0; i < temp.length ; i++) System.out.println(temp[i]); } } ====================================

Remove leading and trailing space from String example


public static void main(String[] args) { String str = " Hello Anuj "; String strTrim = str.trim(); System.out.println("Original String is: " + str); System.out.println("Removed Leading and trailing space"); System.out.println("New String is: " + strTrim); } } =================================

Java String valueOf Example


Java String class defines following methods to convert various Java primitives to Java String object. 1) static String valueOf(int i) Converts argument int to String and returns new String object representing argument int. 2) static String valueOf(float f) Converts argument float to String and returns new String object representing argument float.

3) static String valueOf(long l) Converts argument long to String and returns new String object representing argument long. 4) static String valueOf(double i) Converts argument double to String and returns new String object representing argument double. 5) static String valueOf(char c) Converts argument char to String and returns new String object representing argument char. 6) static String valueOf(boolean b) Converts argument boolean to String and returns new String object representing argument boolean. 7) static String valueOf(Object o) Converts argument Object to String and returns new String object representing argument Object. ====================================

public static void main(String args[]) { int i=10; float f = 10.0f; long l = 10; double d=10.0d; char c='a'; boolean b = true; Object o = new String("Hello World"); System.out.println( String.valueOf(i) ); System.out.println( String.valueOf(f) ); System.out.println( String.valueOf(l) ); System.out.println( String.valueOf(d) ); System.out.println( String.valueOf(c) ); System.out.println( String.valueOf(b) ); System.out.println( String.valueOf(o) ); }