String: - A string is a “collection of characters”. For
example a word or a sentence can be a string. To handle strings, java provides us 3 ways:- 1. String class 2. StringBuffer class 3. Character Array Both String and StringBuffer class are present in java.lang(default) package. Difference between String and StringBuffer:- String StringBuffer 1.It is not flexible in 1.It is flexible in terms of terms of length and length and value both. value stored. 2.String objects are 2.Objects are mutable. immutable(once created it cannot be modified).
Note: - In java we can handle strings by using
character array. According to character array string is collection of characters terminated by null ‘/0’ character. char str[]=new char[20]; str=”roly/0” Creating a String There are two ways: - 1. By using String literals. e.g. String str=”hello”; 2. By using new keyword. e.g. String str=new String(“hello”); Note: - Separate memory is allocated when string objects are created using new operator even if strings are identical. Same memory is allocated when string objects are created using String literal with identical Strings. e.g. 1. String s1=”hello”; String s2=s1; ( reference creation, alternative name, same location) Here s1 and s2 points to same memory location. 2. String s1=new String(“hello”); String s2=new String(“hello”); Here s1 and s2 points to two different locations. String class String is a predefined class defined in java.lang package. It provides various predefined methods as well as predefined constructors to handle strings. Constructors of String class:- 1. Default constructor:- String s1=new String(); S.o.p(“s1=”+s1); Here s1 will initialize with empty value. 2. Parameterized constructor:- (a) String objectname=new String(“value”); e.g. String s1=new String(“hello”); S.o.p(“s1=”+s1);→s1=hello (b) String object=new String(String object); e.g. String s1=new String(“hello”); String s2=new String(s1);→s2=hello (c) char arr[]={‘c’,’l’,’a’,’s’,’s’}; String s1=new String(arr); String objectname=new String(character array); S.o.p(“s1=”+s1);→s1=class (d) String object=new String(character array,start,length);
e.g. char arr[]={‘c’,’l’,’a’,’s’,’s’};
String s1=new String(arr,1,2);→la
Input: - There are three frequently used methods
to take string or character as input. Scanner sc=new Scanner(System.in); 1. sc.next();→used to input single word. 2. sc.nextLine();→used to input multiple words string. 3. sc.next().charAt(0);→used to input single character. Methods of String class String s1=new String(); 1.s1.trim() This function removes the white spaces from the beginning of the string. e.g. String s1=” hello”; String s2=s1.trim(); S.o.p(“s2=”+s2);→s2=hello 2.s1.toLowerCase() This function converts given string into lowercase. e.g. String s1=new String(“HELLO”); String s2=new String(s1.toLowerCase()); S.o.p(“s2=”+s2);→s2=hello 3.s1.toUpperCase() This function converts given string into uppercase. 4.int length() int x=s1.length(); This function returns the total number of characters present in a string. 5.char charAt( int n)→prototype s1.charAt(int n); This function returns the character present at nth position. e.g. s1=”hello”; char x=s1.charAt(3); System.out.println(x);→ l 6. int indexOf(char ch) This function will return the position of first occurrence of character in the string. String s1=”hello”; System.out.println(s1.indexOf(‘l’));→2 7.int lastIndexOf(char ch) This function will return the position of last occurrence of given character in the string. String s1=”hello”; System.out.println(s1.lastIndexOf(‘l’));→3 8.String concat(String str) This method is used to concatenate two strings. e.g. s1=”hello”; s2=”hi”; System.out.println(s1.concat(s2));→hellohi This method adds string s2 at the end of s1. 9.boolean equals(String str) boolean x=s1.equals(s2); This function checks the equality of both strings s1 and s2. If both are equal it will return true value otherwise false. It is case-sensitive also. e.g. s1=”hello”; s2=”Hello”; boolean x=s1.equals(s2); System.out.println(x);→ false 10. boolean equalsIgnoreCase(String str) This function checks the equality of both strings s1 and s2. If both are equal it will return true value otherwise false by ignoring case. 11.int compareTo(String str) This function compares two strings lexicographically. All the characters of both strings are converted to Unicode for comparison. S1=”there”; S2=”their”; Syntax: int x=s1.compareTo(s2); Where x may be 1. 0 if s1=s2 2. +ve if s1>s2 3. –ve if s1<s2
12.int compareToIgnoreCase(String str)
This method works like compareTo() method but it ignores case differences. 13.String replace(char oldchar, char newchar) This method is used to replace characters. It will replace all occurrences of old characters with the new character. Syntax: s1.replace(oldchar,newchar) String s1=new String(“Hello”); String s2=new String(s1.replace(‘e’,’i’)); System.out.println(s2);→hillo 14.s1.substring(int) This function returns a substring from the main string. It will accept the starting point of the substring as an argument. Syntax: s1.substring(int); e.g. s1=”hello world”; s2=s1.substring(6);→world 15.s1.substring(int beginindex, int endindex) This method is also used to return substring from the given string. In this method we can specify the point from where the substring would start and at which it would end. e.g. s1=”Hello world”; s2=s1.substring(3,6);→lo w 16.boolean startsWith(String str) This function checks the string and returns true value if the string begins with the string passed as an argument, otherwise false. e.g. s1=”Hello world”; boolean x=s1.startsWith(“Hello”); System.out.println(x);→true 17.boolean endsWith(String str) s1=”Hello world”; boolean x=s1.endsWith(“world”); System.out.println(x);→true This function checks the string and returns the value as true if the string ends with string passed as an argument otherwise false. 18.String valueOf(any primitive datatype) It is used to convert primitive data type into string type. e.g. String.valueOf(10);→”10”