Java String Array
String[] str_array = new String[2];
str_array[0] = "Java String";
str_array[1] = "Java String Array";
System.out.println("Index 0 : " +str_array);
System.out.println("Index 1 : " +str_array);
Output
Index 0 : Java String
Index 1 : Java String Array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java String - Concatenation
String a = "Java String1";
String b = "Java String2";
System.out.println("Concatenating String: " +a+b);
Output
Concatenating String: Java String1JavaString2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java: Convert String to int
String str_int = "10";
int a = Integer.parseInt(str_int);
//printing a which is integer
System.out.println("Java String to int: " +a);
Output
Java String to int: 10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java Split String
String str_a = "Java String";
//Java String Length
int le = str_a.length();
System.out.println("Length of string is: " +len);
//start index 0
System.out.println("Character at 5: " +str_a.charAt(5));
System.out.println("Characters from 5 till end: " +str_a.substring(5, len));
Output
Length of string is: 11
Character at 5: S
Character from 5 till end: String
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java String Comparison
String a = new String("Java String");
String b = new String("Java String");
if (a == b)
System.out.println("Both Java Strings are equal.");
else
System.out.println("Java Strings are not equal.");
if (a.equals(b))
System.ou.println("Both Java Strings are equal.");
else
System.out.println("Java String are not equal.");
Output
Java String are not equal.
Both Java Strings are equal.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java String Replace
String str_orginal = "A Java String";
String str_replaced = str_original.replace('a','b');
System.out.println("Original String = " +str_original);
System.out.println("Replaced String = " +str_replaced);
Output
Original String = A Java String
Replaced String = A Jbvb String
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java String Upper/Lower Cases
String str_orig = "Java String";
System.out.println("Original String: " +str_orig);
String str_low = str_orig.toLowerCase();
System.out.println("Lower case String: " +str_low);
String str_upper = str_orig.UpperCase();
System.out.println("Upper case String: " +str_upper);
Output
Original String: Java String
Lower case String: java string
Upper case String: JAVA STRING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Java String Trim
String str_a = "Java String";
System.out.println("String is: " +str_a+ " of length: " +str_length());
System.out.println("Trimmed String is: " +str_a.trim()+ " of length: " +str_a.length);
Output
String is: Java String of length: 16
Trimmed String is: Java String of length: 12
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~