Strings created usingnew keyword String s1 = new String(“HELLO”); String s2 = new String(“HELLO”); HELLO 3e25a d s1 3e25ae s 2 HELLO wipr.com confidentia 1
2.
Comparing Strings using== operator What is the output ? public class StringTest{ public static void main(String[] args){ String s1= new String("Hello"); String s2= new String("Hello"); if(s1==s2) System.out.println("String objects referenced are same "); else System.out.println("String objects referenced are not same"); } } Output: String objects referenced are not same wipro.co confidentia 2
3.
Comparing Strings usingequals method What is the output ? public class StringTest{ public static void main(String[] args){ String s1= new String("Hello"); String s2= new String("Hello"); if(s1.equals(s2)) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); } } Output: Strings are equal wipro.co confidentia 3
4.
String class Methods confidentia4 startsWith() – Tests if this string starts with the specified prefix. public boolean startsWith(String prefix) "January".startsWith("Jan"); // true endsWith() - Tests if this string ends with the specified suffix. public boolean endsWith(String suffix) "January".endsWith("ry"); // true
5.
String class Methods(Contd.). confidentia 5 compareTo() - Compares two strings and to know which string is bigger or smaller We will get a negative integer, if this String object is less than the argument string We will get a positive integer if this String object is greater than the argument string. We will get a return value 0(zero), if these strings are equal. public int compareTo(String anotherString) public int compareToIgnoreCase(String str) This method is similar to compareTo() method but this does not take the case of strings into consideration.
String class Methods(Contd.). confidentia 7 public int indexOf(int ch, int fromIndex)- It searches for the character represented by ch within this string and returns the index of first occurrence of this character starting from the position specified by fromIndex public int indexOf(String str, int fromIndex) - Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. String str = “How was your day today?”; str.indexof(‘a’, 6); str(“was”, 2);
8.
String class Methods(Contd.). confidentia 8 lastIndexOf() –It searches for the last occurrence of a particular character or substring substring() - This method returns a new string which is actually a substring of this string. It extracts characters starting from the specified index all the way till the end of the string public String substring(int beginIndex) Eg: "unhappy".substring(2) returns "happy" public String substring(int beginIndex, int endIndex) Eg: "smiles".substring(1, 5) returns "mile“
String class Methods confidentia11 replace()- Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar public String replace(char oldChar, char newChar) "wipra technalagies".replace('a', 'o') returns "wipro technologies"
12.
String class Methods(Contd.). confintial 12 trim() - Returns a copy of the string, with leading and trailing whitespace omitted public String trim() String s = “ Hi Mom! “.trim(); S = “Hi Mom!” valueOf() – This method is used to convert a character array into String. The result is a String representation of argument passed as character array public static String valueOf(char[] data)