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)
s.count(substring, [start, end])— counts occurrences of a substring, with optional start/end.s.expandtabs([tabsize])— replaces tabs with spaces.s.find(substring, [start, end])— returns the index of the first occurrence of a substring, or-1if not found.s.isalnum()— returnsTrueif all characters are alphanumeric,Falseotherwise.s.isalpha()— returnsTrueif all characters are alphabetic,Falseotherwise.s.isdigit()— returnsTrueif all characters are digits,Falseotherwise.s.join(t)— joins the strings in sequencet.s.lower()— converts the string to all lowercase.s.replace(old, new, [maxreplace])— replacesoldsubstring withnewsubstring.s.strip([characters])— removes whitespace, or optional characters.s.split([separator], [maxsplit])— splits a string on whitespace or an optional separator, returning a list.
String Methods (Java)
s.charAt(index)— returns the character at the specified index.s.compareTo(t)— compares stringsto stringt, lexicographically.s.concat(t)— appends stringtto the end of strings.s.contains(t)— checks if stringscontains stringt, returns true/false.s.endsWith(t)— checks if stringsends with stringt.s.equals(t)— checks if stringsis the same as stringt.s.indexOf(t)— searchessfor the first occurrence oftand returns its starting index.s.lastIndexOf(t)— searchessfor the last occurrence oft.s.isEmpty()— returnstrueifsis empty,falseotherwise.String.join(" ", "Orange", "apple", "mango")— joins the strings with the given delimiter between them.s.length()— returns the number of characters in the string.s.replace('l', 'p')— returnsswith every'l'replaced with'p'.s.startsWith(t)— returns true ifsstarts witht.s.subSequence(7, 12)— returns the characters insfrom index 7 to 12 (exclusive).s.substring(7, 12)— returns a substring ofs; if the end argument is omitted, the substring runs to the end of the string.s.trim()— removes whitespace from both sides ofs.