Java String Methods with Syntax and Examples
Syntax Purpose Example
ctor String s = new String(); Create empty string Example: String s = new String();
ctor String s = new String(char[] chars); Create from char array char[] arr = {'a','b'}; String s = new String(arr);
ctor String s = new String(char[] chars, int startIndex, int numChars); Create from part of char array char[] arr = {'a','b','c','d'}; String s = new String(arr, 1, 2);
ctor String s = new String(String another); Create copy of another string String s1 = "Hello"; String s2 = new String(s1);
int len = [Link](); Get length of string String s = "Hello"; int len = [Link]();
char ch = [Link](int index); Get character at position String s = "Java"; char ch = [Link](1); // a
s() [Link](int srcStart, int srcEnd, char[] target, int targetStart); Copy part of string into char array String s = "Hello"; char[] buf = new char[3]; [Link](1, 4
() byte[] arr = [Link](); Get bytes from string String s = "ABC"; byte[] b = [Link]();
rray() char[] arr = [Link](); Convert to char array String s = "Hi"; char[] ch = [Link]();
[Link](Object str); Compare strings (case-sensitive) "Hello".equals("Hello"); // true
noreCase() [Link](Object str); Compare strings ignoring case "HELLO".equalsIgnoreCase("hello"); // true
atches() [Link](int start1, String s2, int start2, int numChars); Compare regions of strings "HelloWorld".regionMatches(0, "Hello", 0, 5); // true
atches(ignore case) [Link](true, int start1, String s2, int start2, int numChars); Compare regions ignoring case "HELLO".regionMatches(true, 0, "hello", 0, 5); // true
h() [Link](String str); Check if starts with given substring "Java".startsWith("Ja"); // true
h(index) [Link](String str, int index); Check start from specific index "HelloWorld".startsWith("World", 5); // true
h() [Link](String str); Check if ends with given substring "Java".endsWith("va"); // true
To() [Link](String str); Compare lexicographically "abc".compareTo("abd"); // negative
) [Link](int ch); Find first occurrence of character "Hello".indexOf('l'); // 2
) [Link](String str); Find first occurrence of substring "Hello".indexOf("lo"); // 3
start) [Link](int ch, int startIndex); Find char from position "Hello".indexOf('l', 3); // 3
start) [Link](String str, int startIndex); Find substring from position "HelloHello".indexOf("lo", 5); // 8
xOf() [Link](int ch); Find last occurrence of character "Hello".lastIndexOf('l'); // 3
xOf() [Link](String str); Find last occurrence of substring "HelloHello".lastIndexOf("lo"); // 8
xOf(start) [Link](int ch, int startIndex); Find last char before position "HelloHello".lastIndexOf('l', 5); // 3
xOf(start) [Link](String str, int startIndex); Find last substring before position "HelloHello".lastIndexOf("lo", 5); // 3
g() [Link](int startIndex); Get substring from position to end "Hello".substring(2); // "llo"
g() [Link](int startIndex, int endIndex); Get substring between positions "Hello".substring(1, 4); // "ell"
[Link](String str); Append string "Hello".concat("World"); // "HelloWorld"
) [Link](char old, char new); Replace characters "Hello".replace('l', 'w'); // "Hewwo"
[Link](); Remove leading/trailing spaces " Hello ".trim(); // "Hello"
Case() [Link](); Convert to lowercase "HELLO".toLowerCase(); // "hello"
Case() [Link](); Convert to uppercase "hello".toUpperCase(); // "HELLO"
) [Link](int num); Convert primitive to string [Link](123); // "123"
) [Link](Object obj); Convert object to string [Link](new Integer(10)); // "10"
) [Link](); Object to string representation Integer i = 5; [Link](); // "5"