indexOf(): ========== Note: ===== 1) indexOf() method can use to search not only the character it can also search about the string. and can retur: the first occurrence value. class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming langauge is hig-level and Object-Oriented Programming Language"; System.out.println(s1.indexOf('a')); System.out.println(s1.indexOf("Program")); System.out.println(s1.indexOf("a")); } } 2) indexOf() can return: "-1" when we can search about unknown element (character/string) in the given string. class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming langauge is hig-level and Object-Oriented Programming Language"; System.out.println(s1.indexOf('A')); } } lastIndexOf(): ============= Note: ==== 1) lastIndexOf() can also use to search about the string in the given string. class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.lastIndexOf('l')); System.out.println(s1.lastIndexOf("Language")); } } 2) lastIndexOf() can return '-1' when the searching object is unknown to the given string. class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.lastIndexOf('A')); } } ============================ # If we are searching about "empty string" in the given string: ================================================================ output : 0 class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.indexOf("")); } } ============================== output: Last Character Index class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.lastIndexOf("")); System.out.println(s1.length()); } } ====================================================== # If we are searching about null value in the given string: =========================================================== indexOf(): ========== Null Pointer Exception class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.indexOf(null)); } } ===================== lastIndexOf(): ============== Null Pointer Exception class IndexOfMethod{ public static void main(String x[]) { String s1 = "Java Programming Language is hig-level and Object-Oriented Programming Language"; System.out.println(s1.lastIndexOf(null)); } } =========================================== startsWith(): ============= Syntax: str-obj.startsWith("searching-object") ==> return : boolean value (true/false) ==> can use to check whether the given string has begin with given group of characters (string) ot not. Url: https://www.ashokit.in // WAP TO CHECK WHETHER THE GIVEN URL IS VALID OR NOT. import java.util.Scanner; class URLValidation{ public static void main(String[] args) { Scanner sco = new Scanner(System.in); System.out.println("Enter the URL:"); String URL = sco.nextLine(); if((URL.startsWith("https://www.")) || (URL.startsWith("http://www."))) { System.out.println("The Given URL is Valid URL"); } else { System.out.println("The Given URL is Invalid URL"); } } } ======================================== endsWith(): ========== Syntax: str-object.endsWith("Searching-object") returns: Boolean Values (true/false) to check wheter the given string ending with specified group of characters (string) or not. Ex: "abcd@gmail.com" or "abcd@rediffmail.com" etc. // WAP TO CHECK WHETHER THE GIVEN MAIL ID IS VALID OR INVALID import java.util.Scanner; class Mailvalidator{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter the user mail id:"); String mail = scan.next(); if((mail.endsWith("@gmail.com"))) { System.out.println("The Given Mail Id is Valid Mail Id."); } else { System.out.println("The Given Mail Id is Invalid mail Id."); } } } ============================================== trim(): ======= " ravi kumar " trim() ==> "ravi kumar" ==> use to trim the blank spaces from the beginning of string and/or from the ending of the string. Syntax: String-object.trim() class TrimmingoBlankSpaces{ public static void main(String[] args) { String s1 = "Hello World!"; String s2 = " Hello Java"; String s3 = "Hi all "; String s4 = " Java Programs "; System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(); System.out.println(s1.trim()); System.out.println(s2.trim()); System.out.println(s3.trim()); System.out.println(s4.trim()); } } ========================================== toCharArray(): ============= "hello" {'h','e','l','l','o'} ==> use to convert a string into a character array. Syntax: str-obj.toCharArray(); class StringToCharacterArray{ public static void main(String[] args) { String s = "Hello World!"; System.out.println(s); char[] ar = s.toCharArray(); // for(char c : s) // { // System.out.println(c); // } for(char c:ar) { System.out.println(c); } } } =========================================== Conversion of Primitive Datatype to String: ========================================== valueOf(): ========== Syntax: String.valueOf(primitive Data variable) class PrimitiveToString{ public static void main(String[] args) { int a = 112; float b = 1.12F; boolean c = true; String s1 = String.valueOf(a); String s2 = String.valueOf(b); String s3 = String.valueOf(c); System.out.println("s1 = "+s1); System.out.println("s2 = "+s2); System.out.println("s3 = "+s3); } } ============================================ String Concatenation: ===================== Joining of two strings into one string is called as "String Concatenation". Symbol: '+' Syntax: Str3 = str1 + str2 class StringConcatenation{ public static void main(String[] args) { int a = 100; String s1 = "Hello"; String s2 = "Java"; String s3 = s1 + s2; String s4 = a + s1; // joining of an integer to the string String s5 = s4 + 123.123; // joining the string to the double value System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s5); System.out.println(s5 + true); // joining of string to the boolean value } }