String.contains — checking for a substring
String.contains checks whether text contains a given sequence of char values. It returns boolean, is case-sensitive, and does not interpret regular expressions.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
String.contains checks whether text contains a given sequence of char values. It returns boolean, is case-sensitive, and does not interpret regular expressions.
Compare a String with a CharSequence or StringBuffer without converting the argument to String.
The `String(String)` constructor creates a distinct object with the same text. Use `==` to compare references and `equals` to compare string contents.
Choose an explicit charset to recover text from bytes and produce repeatable input for a hash. Learn which overloads replace invalid input and how to reject it instead.
String.equals compares string contents with case sensitivity. The == operator compares object identity rather than contents.
String.equalsIgnoreCase compares strings without distinguishing letter case, independently of locale. It returns boolean.
String.format builds text from a format string and arguments using specifiers such as %s, %d, and %.2f. An explicit Locale makes numeric formatting independent of the environment's default locale settings.
String.hashCode returns an int hash code calculated from a string's contents. Equal strings have equal hashes, but matching hashes do not prove that strings are equal.
String.indexOf returns the position of the first substring match or -1. The fromIndex parameter lets you start searching at a specified position.
Learn why two strings with the same contents can be different objects, how `intern()` selects a canonical object, and what `length()` actually counts.
String.isBlank checks whether a string is empty or contains only whitespace as defined by Character.isWhitespace(int). It returns boolean and has been available since Java 11.
String.isEmpty() checks whether a string has length zero. It does not treat spaces as empty text and requires a non-null reference; use isBlank() to check for empty text or text consisting only of whitespace.
String.join builds text from elements separated by a delimiter. It accepts an Iterable or varargs arguments.
StringJoiner builds text with a delimiter, prefix, and suffix. merge preserves the incoming content's internal delimiter.
String.length() returns the number of UTF-16 code units, not necessarily the number of user-visible characters.
Test whether an entire string matches a regular expression. Learn how `(?s)` changes the dot, which character classes default to ASCII, and how to write regex backslashes in Java string literals.
String.replace replaces all literal matches without regular expressions. It returns the result without changing the original.
Replace every match of a regular expression. The example masks values in one specific password-argument format without changing the inputs.
String.split returns an array of text fragments separated by regular-expression matches. The limit parameter controls the number of splits and the treatment of trailing empty elements.
String.startsWith checks whether a string begins with a given prefix, optionally at a specified index.
String.substring returns part of a string without changing the original. The start is inclusive and the end exclusive; indices count UTF-16 code units.
String.toLowerCase returns lowercase text. The choice of Locale affects the result; use Locale.ROOT for language-independent text.
String.toUpperCase returns uppercase text. The chosen Locale affects the result; Locale.ROOT provides language-neutral rules.
String.trim removes characters with values no greater than U+0020 from both ends, leaving the interior unchanged.