Java String Methods
Java String Methods
Java String
• Syntax:
• String variableName = “String”;
System.out.println(string1.equalsIgnoreCase(string2));
//Output: true
String Methods
The .startsWith() method checks the string starts with the specified
prefix.
ex.
String string1 = "Hello";
The .endsWith() method checks the string ends with the specified
prefix.
ex.
String string1 = "Hello";
System.out.println(string1.toUpperCase());
//Output: HELLO WORLD!
String Methods
System.out.println(string1.toLowerCase());
//Output: hello world!
String Methods
System.out.println(string1.trim());
//Output: hello world!
String Methods
System.out.println(string1.indexOf(“o”));
//Output: 4
System.out.println(string1.indexOf(“llo”));
//Output: 2
System.out.println(string1.indexOf(“abc”));
//Output: -1
String Methods
System.out.println(string1.contains(“llo”));
//Output: true
System.out.println(string1.indexOf(“hi”));
//Output: false
String Methods
The .concat() method combine the specified string to the end of the
first string.
ex.
String string1 = "hello”;
String string2 = “world”
System.out.println(string1.concat(string2));
//Output: helloworld
String Methods
The .concat() method combine the specified string to the end of the
first string.
ex.
String string1 = "hello”;
String string2 = “world”
System.out.println(string1.concat(“kitty”));
//Output: hellokitty
String Methods
System.out.println(string1.isEmpty());
//Output: true
System.out.println(string2.isEmpty());
//Output: Error
String Methods
System.out.println(string1.substring(2));
//Output: llo world
System.out.println(string1.substring(2,8));
//Output: llo wo
String Methods
System.out.println(string1.replace(‘l’, ‘*’));
//Output: he**o wor*d
System.out.println(string1.replace(“ll”, ‘*’));
//Output: he*o world