- We can compare String in Java on the basis of content a
- It is used in
- authentication (by equals() method),
- sorting (by compareTo() method),
- reference matching (by == operator).
- There are three ways to compare String in Java:
- == :operator always ment for reference comparision
- String : CompareTo() vs .equals()
Ratan Ratan=True Rattan Anu=false CompareTo() ------ int R=82 A=65 Ratan Ratan=0 Ratan Anu=17 Anu Ratan=-17 Example of “String Comparision” Program 12 class compare{ public static void main(String args[]){ String s1=“anu"; String s2=“anu"; System.out.println(s1.equals(s2)); System.out.println(s1==s2); String s3=new String(“bharath"); String s4=new String(“bharath"); System.out.println(s3.equals(s4)); System.out.println(s3==s4); } } - class compare{
- public static void main(String args[]){
- String s1=“Anu";
- String s2=“Ratan";
- System.out.println(s1.compareTo(s2));//17
- System.out.println(s2.compareTo(s1));///-17
- Methods for modifying a String objects.
- substring()
- susubstring(int beginIndex)
- bstring(int beginIndex, int endIndex)
- concat() -- We can concatenate two strings using concat( )
- replace()
- String replace(char original, char replacement)
- trim()
- IndexOf()
- lastIndexOf()
- toUpperCase()
- toLowerCase()
class Methods{ class Methods{
Do'stlaringiz bilan baham: |