Strings (Icse) : I) String Ii) Stringbuffer
Strings (Icse) : I) String Ii) Stringbuffer
Q. What is a string?
Ans. A string is a sequence of zero or more characters enclosed in double quotes.
Creation:
Q. How are strings created in Java?
Ans. A string is created using the String class constructor.
Syntax: String stringName = new String([parameter]) ;
For e.g.
String s = new String( ) ; // creates an empty string
String s = new String(“Dell”) ; // creates string “Dell”
Initialization:
(i) Using a String Literal:
String s = “ ” ; // initializes an empty string
String s = “Java” ; // initializes string “Java”
(ii) Using Scanner Object:
String s = sc.next( ) ; // inputs a single word
String s = sc.nextLine( ) ; // inputs a text
Q. Write a statement to print the number of characters present in the String object str.
Ans. System.out.println(str.length( )) ;
length length( )
It is an instance variable. It is an instance method.
It gives the length of an array. It gives the length of a string.
2. char charAt(int i): It returns the character at the specified index ‘i’ of the given string. For
e.g.
i) String x = “School” ;
char c = x.charAt(3) ; // ‘o’
ii) String y = ‘Application”
System.out.println(y.charAt(4)) ;
iii) System.out.println(“Science”. charAt(2)) ;
Q. Write a Java statement to extract the second last character of word stored in wd.
Ans. char c = wd.charAt(wd.length( ) – 2) ;
3. int indexOf(int i) or int indexOf(int ch, int i): It returns the index position of the 1st
occurrence of the specified
character within the given string. For e.g.
String x = “malayalam” ;
i) System.out.println(x.indexOf(‘a’)) ; // 1
ii) System.out.println(“Chennai”.indexOf(‘n’)) ;//3
iii) System.out.println(x.indexOf(‘a’,2)) ;//3
charAt( ) indexOf( )
It takes one int type parameter. It takes one or two int type parameters.
It returns the character at the specified index. It returns the index of the specified character.
4. int lastIndexOf(int ch): It returns the index position of the last occurrence of the specified
character in the given
string. For e.g.
i) String x = “Mississippi” ;
System.out.println(x.lastIndexOf(‘s’)) ; //6
ii) System.out.println(“Tomorrow”.lastIndexOf(‘o’)) ;//6
5. String substring(int start) or String substring(int start, int end): It extracts the substring
between a specified start index and end index of a given string.
i) String x = “Interesting” ;
System.out.println(x.substring(4)) ; // “resting”
ii) System.out.println(“Applications”.substring(5,8)) ; // “cat”
6. String replace(char oldch, char newch): It returns the substring after replacing all
occurrences of an existing old character with the specified new character of a given string. For
e.g.
i) String x = “cellar den” ;
String y = x.replace(‘e’,‘o’) ; // “collar don”
ii) System.out.println(“Two”.replace(‘w’,‘o’)) ;//Too
7. boolean equals(String obj): It returns true, if two strings are exactly same, including their
case. For e.g.
i) String x = “hello” ; String y = “world” ;
System.out.println(x.equals(y)) ;
ii) System.out.println(“Delhi”.equals(“delhi”)) ;
equals( ) ==
It is an accessor method of String class. It is a relational equality operator.
It checks if two string objects are same or not. It checks if two object references refer to the same memory location or not.
8. boolean equalsIgnoreCase(String obj): It returns true, if two strings are exactly same, but
ignores their case. For
e.g.
String x = “Exam” ;
System.out.println(x.equalsIgnoreCase(“exam”));// true
9. int compareTo(String s2): It compares two strings of same case in alphabetical order and
returns a value < 0 (s1
< s2), > 0 (s1 > s2), 0 (s1 = s2). For e.g.
i) String x = “track” ; String y = “trick” ;
System.out.println(x.compareTo(y)) ; // -8
ii) System.out.println(“jai”.compareTo(“jagat”)) ; // 2
iii) System.out.println("cat".compareTo("cation")); // -3
equals( ) compareTo( )
It returns a boolean value. It returns an integer value.
It checks two strings for equality. It compares two strings in alphabetical order.
10. String concat(String str): It returns a new string after joining a given string with
another specified string. For e.g.
i) String x = “New” ; String y = “Orleans” ;
System.out.println(x.concat(“ Delhi”)) ; // "New Delhi"
ii) String z = x.concat(y) ;
System.out.println(z) ; // “NewOrleans”
13. boolean startsWith(String str): It returns true, if a given string starts with a specified string.
For e.g.
S.o.pln(“Cell Wall” . startsWith(“Cell”)) ;// true
14. boolean endsWith(String str): It returns true, if a given string ends with a specified string.
For e.g.
i) System.out.println(“Chocobar” . endsWith(“bar”)) ;// true
ii) String x = “Science is Fun” ;
System.out.println(x.endsWith(“Fun”)) ;//true
15. String toLowerCase( ): It returns a new string after converting all upper case letters of a
given string to lower
case. For e.g.
String x = “KnOwLEDge” ; System.out.println(x.toLowerCase( )) ;
16. String toUpperCase( ): It returns a new string after converting all lower case letters of a
given string to upper
case. For e.g. System.out.println(“KnOwLEDge”.toUpperCase( )) ;
17. String valueOf(arg): It returns the string representation of the given argument. For e.g.
int x = 123 ; double y = 0.5 ;
String s = String.valueOf(x) + String.valueOf(y) ;
System.out.println(s) ; // “1230.5”
18. char[ ] toCharArray( ): It transfers all characters of a String into a character array. For e.g.
String str = “BlueJ” ;
char ch[ ] = str.toCharArray( ) ;
System.out.println(ch[0] = = str.charAt(4)) ;