We have below list of methods available on String object in java.
public class StringMethodsTest {
public static void main(String[] args) {
String test="java";
//charAt()
System.out.println(test.charAt(0));//index will start with zero
//concat()
test.concat("program");
System.out.println(test);//will print java becasue we just concatinated but did not assign to string
String testcon = test.concat("program");
System.out.println(testcon);
//contains
System.out.println(test.contains("j"));//return true if string contains char j any where//trur
System.out.println(test.contains("jv"));//return true if string contains char j any where//false
//ends with
System.out.println(test.endsWith("a"));//return true if specified string is ends with mentioed suffix//true
System.out.println(test.endsWith("aj"));//return true if specified string is ends with mentioed suffix//false
//equals
String test1 = "java";
System.out.println(test.equals(test1));//will check only text here//true
//compareTo
//ifs1==s2, then 0 or if s1>s2 then +ve or if s1 <s2, then -ve values will print
String test3 = "sql";
String test4 = "c++";
String test5 = "jquery";
System.out.println(test.compareTo(test3));//-ve value-9 --diffrence between first characters
System.out.println(test.compareTo(test4));//+ve value 2--diffrence between first characters
System.out.println(test.compareTo(test1));//here 0
System.out.println(test.compareTo(test5));//here -ve -16 becasue second character is different
//equalIgnorecase
String test6 = "JAVA";
System.out.println(test.equalsIgnoreCase(test6));//ignore case sensitive//true
System.out.println(test.equals(test6));//not case sensitive//false
}
}
Will continue other methods in next post.
public class StringMethodsTest {
public static void main(String[] args) {
String test="java";
//charAt()
System.out.println(test.charAt(0));//index will start with zero
//concat()
test.concat("program");
System.out.println(test);//will print java becasue we just concatinated but did not assign to string
String testcon = test.concat("program");
System.out.println(testcon);
//contains
System.out.println(test.contains("j"));//return true if string contains char j any where//trur
System.out.println(test.contains("jv"));//return true if string contains char j any where//false
//ends with
System.out.println(test.endsWith("a"));//return true if specified string is ends with mentioed suffix//true
System.out.println(test.endsWith("aj"));//return true if specified string is ends with mentioed suffix//false
//equals
String test1 = "java";
System.out.println(test.equals(test1));//will check only text here//true
//compareTo
//ifs1==s2, then 0 or if s1>s2 then +ve or if s1 <s2, then -ve values will print
String test3 = "sql";
String test4 = "c++";
String test5 = "jquery";
System.out.println(test.compareTo(test3));//-ve value-9 --diffrence between first characters
System.out.println(test.compareTo(test4));//+ve value 2--diffrence between first characters
System.out.println(test.compareTo(test1));//here 0
System.out.println(test.compareTo(test5));//here -ve -16 becasue second character is different
//equalIgnorecase
String test6 = "JAVA";
System.out.println(test.equalsIgnoreCase(test6));//ignore case sensitive//true
System.out.println(test.equals(test6));//not case sensitive//false
}
}
Will continue other methods in next post.
No comments:
Post a Comment