String Indexing: ================ We can use an index (positive values) to access the individual characters of the string. Syntax: string-object-name[index-value]; ==> Not avaiable Note: ===== indexing notation "[]" is not supported for the strings Acquiring the Part of string: ============================= part of string ==> sub-string substring(): ============ ==> can be used to access or to acquire the part of the string. ==> in two ways: 1) substring(startIndex) 2) substring(startIndex, endIndex) Syntax: string-object.substring() class StringIndexing{ public static void main(String[] args) { String s1 = "Java Programming"; System.out.println(s1); System.out.println(s1.substring(5)); System.out.println(s1.substring(0,4)); } } ===================================== String Comparison: ================== 1) using == (equal operator) 2) equals() 3) equalsIgnoreCase() 4) compareTo() 1) using == (equal operator) ============================= class StringComparison{ public static void main(String[] args) { String s1 = "java"; // string literal String s2 = new String("java"); // string object // == : compare whether two strings pointing to the same object or not System.out.println(s1 == s2); String s3 = s1; String s4 = s2; System.out.println(s1 == s3); System.out.println(s2 == s4); } } ================================== equals(): ========= ==> pre-defined method use to compare whether two strings are with same data or not. Syntax: string-object1.equals(string-object2) class StringComparison{ public static void main(String[] args) { String s1 = "Java"; String s2 = s1; String s3 = new String("Java"); String s4 = s3; String s5 = null; System.out.println(s1.equals(s2)); System.out.println(s3.equals(s4)); System.out.println(s1.equals(s3)); System.out.println(s1.equals(s5)); } } ====================================== 3) equalsIgnoreCase() ===================== s1 = "java" s2 = "Java" s3 = "JAVA" s1.equalsIgnoreCase(s2) ==> true ==> used to compare the internal content of two strings are same or not by ignoring the case. Syntax: string1.equalsIgnoreCase(string2) class StringComparison{ public static void main(String[] args) { String s1 = "Java Programming Language"; String s2 = new String("java programming language"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println(s1.equalsIgnoreCase(s2)); } } =================================== 4) compareTo(): =============== return an integer value: 0 ==> both strings are same else ==> both strings are not same Syntax: string1.compareTo(string2) class StringComparison{ public static void main(String[] args) { String s1 = "java"; String s2 = new String("java"); String s3 = "Java"; String s4 = "python"; System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s3)); System.out.println(s3.compareTo(s2)); System.out.println(s1.compareTo(s4)); } } ========================================== String Length: ============== length(): ========= ==> use to find the size or the string or ==> to count the number of characters in a string, we can use length() method. Syntax: string-object-name.length() class StringComparison{ public static void main(String[] args) { String s1 = new String("Java Programming Language"); System.out.println("The Size of the String = "+s1.length()); } } ======================================= Mutable Vs Immutable: ===================== Immutable: ========== The object which does not allowed for the modification after the definition are called as "Immutable Objects". Mutable: ======== The Object which does allowed for the modification after the definition are called as "Mutable Objects". Strings Vs Immutable: ===================== concat(): ========= ==> string concatenation ==> joining of two strings into one is called as "string concatenation". Syntax: string1.concat(string2); ex: s1 = "java" s2 = "program" concat() ==> "javaprogram" class StringImmutability{ public static void main(String[] args) { String s1 = "Java"; String s2 = "Programs"; System.out.println("Before the Concatenation:"); System.out.println(s1); System.out.println(s2); // String s3 = s1.concat(s2); s1 = s1.concat(s2); System.out.println("After the Concatenation:"); System.out.println(s1); System.out.println(s2); // System.out.println(s3); } } =============================== toUpperCase(): ============== ==> can use to convert the string from lower case to upper case. Syntax: string-object.toUpperCase() toLowerCase(): ============== ==> can use to convert the string from upper case to lower case. Syntax: string-object.toLowerCase() class StringImmutability{ public static void main(String[] args) { String s1 = "java programming"; String s2 = "PYTHON"; System.out.println("Before to change:"); System.out.println(s1); s1.toUpperCase(); System.out.println("After the change:"); System.out.println(s1); s1 = s1.toUpperCase(); System.out.println(s1); System.out.println("s2 = "+s2); s2 = s2.toLowerCase(); System.out.println("s2 = "+s2); } } ============================ class StringImmutability{ public static void main(String[] args) { String username = "Ravi123"; String password = "ravi@123"; if((username.equals("Ravi@123")) && (password.equals("ravi@123"))) { System.out.println("Login Successful"); } else { System.out.println("Login Fail"); } } }