Methods of class String
Last updated
Last updated
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String name = new String("Paul");
System.out.println(name.charAt(0));
System.out.println(name.charAt(2));
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String name = new String("ABCAB");
System.out.println(name.indexOf('B'));
System.out.println(name.indexOf("S"));
System.out.println(name.indexOf("CA"));
System.out.println(name.indexOf("B", 2));
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String exam = "Oracle";
String sub = exam.substring(2);
String result = exam.substring(2, 4);
System.out.println(sub);
System.out.println(result);
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String varWithSpaces = " AB CB ";
System.out.print(":");
System.out.print(varWithSpaces);
System.out.println(":");
System.out.println("---after trim---");
System.out.println(varWithSpaces.trim());
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String letters = "ABCAB";
System.out.println(letters.replace('B', 'b'));
System.out.println(letters.replace("CA", "12"));
System.out.println(letters);
}
}
Output:
AbCAb
AB12B
ABCAB
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String letters = "ABCAB";
System.out.println(letters.length());
}
}
length of a string is always = lastIndex+1
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String letters = "ABCAB";
System.out.println(letters.startsWith("AB"));
System.out.println(letters.startsWith("a"));
System.out.println(letters.startsWith("A", 3));
System.out.println(letters.endsWith("CAB"));
System.out.println(letters.endsWith("B"));
System.out.println(letters.endsWith("b"));
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String result = "Sunday ".replace(' ', 'Z').trim().concat("M n");
System.out.println(result);
}
}
package com.test.test1;
public class StringMethods {
public static void main(String[] args) {
String day = "SunDday";
day.replace('D', 'Z').substring(3);
System.out.println(day);
day = day.replace('D', 'Z').substring(3);
System.out.println(day);
}
}