String Methods Reference - Syntax and Description
Explore all Java String methods with detailed syntax and easy-to-understand descriptions. Learn how to manipulate strings in Java efficiently.
This reference explains String Methods Reference - Syntax and Description through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
String Methods Reference - Syntax and Description
- charAt()
Returns the character at the specified index in the string.public char charAt(int index)- codePointAt()
Returns the Unicode code point at the specified index.public int codePointAt(int index)- codePointBefore()
Returns the Unicode code point before the given index.public int codePointBefore(int index)- codePointCount()
Counts Unicode code points in the specified text range.public int codePointCount(int beginIndex, int endIndex)- compareTo()
Compares two strings lexicographically.public int compareTo(String anotherString)- compareToIgnoreCase()
Compares two strings ignoring case differences.public int compareToIgnoreCase(String str)- concat()
Concatenates the specified string to the end of this string.public String concat(String str)- contains()
Checks if the string contains the specified sequence.public boolean contains(CharSequence s)- contentEquals()
Compares this string to another sequence or buffer for content equality.public boolean contentEquals(CharSequence cs) public boolean contentEquals(StringBuffer sb)- copyValueOf()
Returns a string that contains characters from a character array.public static String copyValueOf(char[] data) public static String copyValueOf(char[] data, int offset, int count)- endsWith()
Checks if the string ends with the specified suffix.public boolean endsWith(String suffix)- equals()
Checks if this string is equal to another object.public boolean equals(Object anObject)- equalsIgnoreCase()
Checks if this string is equal to another string, ignoring case.public boolean equalsIgnoreCase(String anotherString)- format()
Returns a formatted string using specified format string and arguments.public static String format(Locale l, String format, Object... args) public static String format(String format, Object... args)- getBytes()
Encodes this string into a byte array using specified charset or default.public byte[] getBytes() public byte[] getBytes(Charset charset) public void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) public byte[] getBytes(String charsetName)- getChars()
Copies characters from the string into a destination array.public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)- hashCode()
Returns the hash code of the string.public int hashCode()- indexOf()
Finds the index of a character or substring in the string.public int indexOf(int ch) public int indexOf(int ch, int fromIndex) public int indexOf(String str) public int indexOf(String str, int fromIndex)- intern()
Returns the canonical representation of the string from the string pool.public String intern()- isEmpty()
Checks if the string is empty (length is 0).public boolean isEmpty()- join()
Joins multiple strings using the specified delimiter.public static String join(CharSequence delimiter, CharSequence... elements) public static String join(CharSequence delimiter, Iterable extends CharSequence> elements)- lastIndexOf()
Returns the last index of a character or substring.public int lastIndexOf(int ch) public int lastIndexOf(int ch, int fromIndex) public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex)- length()
Returns the length of the string.public int length()- matches()
Checks if the string matches a regular expression.public boolean matches(String regex)- offsetByCodePoints()
Returns the index within the string after codePointOffset code points.public int offsetByCodePoints(int index, int codePointOffset)- regionMatches()
Tests if two string regions match.public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) public boolean regionMatches(int toffset, String other, int ooffset, int len)- replace()
Replaces all occurrences of one character or sequence with another.public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement)- replaceAll()
Replaces each substring that matches a regex with the replacement.public String replaceAll(String regex, String replacement)- replaceFirst()
Replaces the first substring matching regex with the replacement.public String replaceFirst(String regex, String replacement)- split()
Splits the string based on a regex delimiter.public String[] split(String regex) public String[] split(String regex, int limit)- startsWith()
Checks if the string starts with the given prefix.public boolean startsWith(String prefix) public boolean startsWith(String prefix, int toffset)- subSequence()
Returns a character sequence between specified indexes.public CharSequence subSequence(int beginIndex, int endIndex)- substring()
Returns a new string that is a substring of this string.public String substring(int beginIndex) public String substring(int beginIndex, int endIndex)- toCharArray()
Converts the string to a new character array.public char[] toCharArray()- toLowerCase()
Returns a lowercase version of the string.public String toLowerCase() public String toLowerCase(Locale locale)- toString()
Returns the string itself.public String toString()- toUpperCase()
Returns an uppercase version of the string.public String toUpperCase() public String toUpperCase(Locale locale)- trim()
Removes leading and trailing whitespace from the string.public String trim()- valueOf()
Converts different types of data into their string representation.public static String valueOf(boolean b) public static String valueOf(char c) public static String valueOf(char[] data) public static String valueOf(char[] data, int offset, int count) public static String valueOf(double d) public static String valueOf(float f) public static String valueOf(int i) public static String valueOf(long l) public static String valueOf(Object obj)
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.