Skip to content
thesarfo

Reference

String Methods (Python & Java)

Reference for Python's and Java's built-in string methods.

views 0

Strings are immutable sequence objects, with each character an element in the sequence. As with all objects, methods perform operations — but since strings are immutable, no method changes the instance; each simply returns a value that can be stored or passed on.

String Methods (Python)

  1. s.count(substring, [start, end]) — counts occurrences of a substring, with optional start/end.
  2. s.expandtabs([tabsize]) — replaces tabs with spaces.
  3. s.find(substring, [start, end]) — returns the index of the first occurrence of a substring, or -1 if not found.
  4. s.isalnum() — returns True if all characters are alphanumeric, False otherwise.
  5. s.isalpha() — returns True if all characters are alphabetic, False otherwise.
  6. s.isdigit() — returns True if all characters are digits, False otherwise.
  7. s.join(t) — joins the strings in sequence t.
  8. s.lower() — converts the string to all lowercase.
  9. s.replace(old, new, [maxreplace]) — replaces old substring with new substring.
  10. s.strip([characters]) — removes whitespace, or optional characters.
  11. s.split([separator], [maxsplit]) — splits a string on whitespace or an optional separator, returning a list.

String Methods (Java)

  1. s.charAt(index) — returns the character at the specified index.
  2. s.compareTo(t) — compares string s to string t, lexicographically.
  3. s.concat(t) — appends string t to the end of string s.
  4. s.contains(t) — checks if string s contains string t, returns true/false.
  5. s.endsWith(t) — checks if string s ends with string t.
  6. s.equals(t) — checks if string s is the same as string t.
  7. s.indexOf(t) — searches s for the first occurrence of t and returns its starting index.
  8. s.lastIndexOf(t) — searches s for the last occurrence of t.
  9. s.isEmpty() — returns true if s is empty, false otherwise.
  10. String.join(" ", "Orange", "apple", "mango") — joins the strings with the given delimiter between them.
  11. s.length() — returns the number of characters in the string.
  12. s.replace('l', 'p') — returns s with every 'l' replaced with 'p'.
  13. s.startsWith(t) — returns true if s starts with t.
  14. s.subSequence(7, 12) — returns the characters in s from index 7 to 12 (exclusive).
  15. s.substring(7, 12) — returns a substring of s; if the end argument is omitted, the substring runs to the end of the string.
  16. s.trim() — removes whitespace from both sides of s.