1.

State the purpose and return data type of the following String functions :(i) indexOf(). (ii) compareTo().

Answer»

(i) This method returns the first occurance of a character or a sub-string in the string. It returns integer value. 

e.g. : int r = s1.indexOF(“DAY”); 

string s1 = “HAPPY BIRTHDAY”; 

System.out.println(r); Output : 11 

(ii) This method compare the invoking string with string object str exicographically & returns an integer value. 

e.g. : 

string s1 = “GOD”; if (s1.compare To (“OQ”)= = 0) 

System.out.println (“equal”); 

Output : <Nothing>

The result of the comparison is returned and is interpreted as shown here :

Value returnedMeaning
Less than zeroThe invoking string is less than str.
Greater than zeroThe invoking string is greater than str
ZeroThe two string are equal.



Discussion

No Comment Found

Related InterviewSolutions