Page 267 - Cs_withBlue_J_C11_Flipbook
P. 267
Let us see the below example:
String sen1= "Welcome to our school";
String sen2= "WELCOME to our school";
int value= sen1.compareToIgnoreCase(sen2);
System.out.println("Value: "+ value);
Output:
Value: -2
The endsWith(String str) Function
This function checks whether the string in the current object ends with the string given as a parameter. This function
returns a Boolean-type value. The syntax is:
boolean <variable> = String_datatype_Variable.endsWith(String str);
Let us see the below example:
String sen1= "We are studying in class 11";
String sen2= "11";
boolean b=sen1.endsWith(sen2);
if(b)
System.out.println(sen1+ " ends with " + sen2 );
else
System.out.println(sen1+ " does not end with " + sen2);
Output:
We are studying in class 11 ends with 11
The startsWith(String str) Function
This function checks whether the string in the current object starts with the string given as parameter. This function
returns a Boolean type value. The syntax is:
boolean <variable> = String_datatype_Variable.startsWith(String str);
Let us see the below example:
String str1="0", str2="0";
String sen1= "We are studying in class 11";
String sen2= "we";
boolean b=str1.startsWith(str2);
if(b)
System.out.println(sen1+ " begins with " + sen2);
else
System.out.println(sen1+ " does not begin with " + sen2);
The output of the preceding program is as follows:
We are studying in class 11 begins with we
The trim() Function
This function is used to delete the leading and trailing spaces from the string of the current object. If there is no space,
then there will be no change. Also, it does not affect the spaces between the words. This function returns a value of
the String type. The syntax of the trim() method is:
String <variable> = String_datatype_Variable.trim();
Let us see the below example:
String sen1= " Computer Science ";
System.out.println(sen1.trim());
265
Strings 265

