Accessing the String Character based on the Index: ================================================== charAt(): ======== ==> which can accept an index value as parameter/argument and return a character at that index. Syntax: string-object.charAt(Index Value) StringIndexOutOfBoundsException =============================== when we can use the index which is from the outside the range to access the characters of the given string, we can get "StringIndexOutOfBoundsException". class CharacterAccess{ public static void main(String[] args) { String s1 = "Hello Java"; System.out.println("The Total number iof characters = "+s1.length()); System.out.println("The Character from the string = "+s1.charAt(5)); System.out.println("The Character from the string = "+s1.charAt(8)); // System.out.println("The Character from the string = "+s1.charAt(10)); } } ======================================= // WAP TO ACCESS ALL THE CHARACTERS OF THE GIVEN STRING INCLUDING THE INDEX VALUE. /* CHARACTER AT AN INDEX 0 = R */ class StringLooping{ public static void main(String[] args) { String s1 = new String("Hello"); System.out.println("The Charcaters of the string are:"); for(int index = 0;index < s1.length();++index) { System.out.println("Character at an index "+index+" is = "+s1.charAt(index)); } } } ========================================= Conversion of string into an array: =================================== split(): ======= Syntax: string-object.split("Separator"); class StringToArra{ public static void main(String[] args) { String s1 = "Java is high level programming langauge"; String s2 = "05-11-2024"; String[] arr = s1.split(" "); String[] a1 = s2.split("-"); // space ==> \\s // integer ==> \\d for(String s:arr) { System.out.println(s); } for(String i:a1) { System.out.println(i); } } } ============================================== Array To String Conversion: =========================== {"22","11","2024"}; "22-11-2024" "22.11.2024" "22/11/2024" join(): ====== Syntax: String.join("separator",array_name) ==> we required to join members of the array into string by considering the separator. class ArrayToString{ public static void main(String[] args) { String x[] = {"Java","is","high","level","programming","langauge"}; String[] y = {"05","11","2024"}; String s1 = String.join(" ",x); String s2 = String.join("-",y); System.out.println("String s1 = "+s1); System.out.println("String s2 = "+s2); } } ============================================ indexOf(): ========== ==> can accept a character of the string ==> returns the first occurring index value. "hello" 'l' ==> 2,3 Syntax: string-object.indexOf(character); String-object.indexOf(character, s-index) lastIndexOf(): ============= ==> can accept the character of the string ==> returns the last occurring index value. Syntax: string-object.lastIndexOf(character) class StringIndex{ public static void main(String[] args) { String s1 = "hello"; System.out.println(s1.indexOf('l')); System.out.println(s1.indexOf('o')); System.out.println(s1.lastIndexOf('l')); } } ================================== class StringIndex{ public static void main(String[] args) { String s1 = "Java is High Level Programming Langauge"; // 1, 3, 24, 32, 35 System.out.println(s1.indexOf('a',2)); System.out.println(s1.indexOf('a',20)); } } ================================================= String Replacement: =================== "Java is High-Level Programming language" "High-Level" ==> "Object-Oriented" "Java is Object-Oriented Programming Language". 1) replace() 2) replaceFirst() 3) replaceAll() 1) replace() ============ Syntax: String-object.replace(old-character, new-character) class StringReplacement{ public static void main(String[] args) { String s1 = "hello world"; String s2 = s1.replace('l','s'); System.out.println(s1); System.out.println(s2); } } Syntax: str-object.replace(old-string, new-string) class StringReplacement{ public static void main(String[] args) { String s1 = "Java is High-Level Programming Language"; System.out.println(s1); String s2 = s1.replace("High-Level","Object-Oriented"); System.out.println(s2); } } 2) replaceFirst() ================== Syntax: str-object.replaceFirst(old-string, new-string); class StringReplacement{ public static void main(String[] args) { String s1 = "cat is walking on wall, cat is Jumping to another wall."; String s2 = s1.replace("cat","Monkey"); String s3 = s1.replaceFirst("cat","Dog"); // String s4 = s1.replaceFirst('a','A'); System.out.println(s1); System.out.println(s2); System.out.println(s3); } } ===================================== 3) replaceAll() ============== Syntax: String-object.replaceAll(old-string, new-string) class StringReplacement{ public static void main(String[] args) { String s1 = "Hello"; // String s2 = s1.replaceAll('l','L'); String s2 = s1.replaceAll("l","L"); System.out.println(s1); System.out.println(s2); } }