4 - Strings
4 - Strings
▪ String concat()
▪ = assignment, == ,equality,relational
▪ String x=“Computer”;
▪ String y=“Applications”;
▪ String z=x.concat(y);//ComputerApplications
▪ Z=x+y // ComputerApplications
▪ boolean equals(string s)
▪ Ex:
▪ if(x.equals(y)) //false(case sensitive)
equals() vs compareTo()
▪ String x=“Science”;
▪ String y=“SCIENCE”;
▪ boolean equalsIgnoreCase(String s)
▪ boolean c=x.equalsIgnoreCase(y);
//true(ignores the case)
▪ int compareTo(String s)
▪ Returns 0 if both strings are equal , a negative
value st1<st2, a positive number, if st1>st2
▪ int compareToIgnoreCase(String str)
trim(),startsWith(),endsWith()
▪ int n;
▪ String s=“245”;
▪ n=Integer.parseInt(s); //n=245(int form)
▪ n=Integer.valueOf(s); //n=245(int form)
▪ ---------------------------------
▪ String valueOf(primitive type)
▪ int n1=134;
▪ float n2=12.386;
▪ String s1=String.valueOf(n1);// 134 in String form
▪ String s2=String.valueOf(n2);// 12.386 in String form