easyStrings 0 views

How Strings Work — API Cheat Sheet

No exercise here — just the string methods you actually reach for in an interview, side by side across Java, C#, Python, and JavaScript/TypeScript.

This is a reference page, not a graded exercise — there's nothing to run. The problem most people hit in an interview isn't the algorithm, it's freezing on "wait, what's the method called again?" with no autocomplete to bail them out. Read this once, then come back to it before your next mock interview.

Every language here treats strings as immutables.toUpperCase(), s.Replace(...), s.replace(...) etc. all return a new string, they never modify s in place. If you find yourself calling a string method and not using its return value, that's almost always a bug.

Java — String

Want to...MethodExample
Get length.length()s.length()
Get char at index.charAt(i)s.charAt(0)
Slice out a substring.substring(start) / .substring(start, end)s.substring(1, 3)
Find a substring.indexOf(sub) / .lastIndexOf(sub)s.indexOf("lo") (-1 if not found)
Check if it contains.contains(sub)s.contains("lo")
Compare for equality.equals(other) / .equalsIgnoreCase(other)s.equals(t)never use == on strings in Java
Split on a delimiter.split(regex)s.split(",")String[]
Trim whitespace.trim() / .strip()s.strip()
Change case.toUpperCase() / .toLowerCase()s.toLowerCase()
Replace.replace(a, b)s.replace("a", "b")
To a char array.toCharArray()char[] cs = s.toCharArray();
Join a list of stringsString.join(sep, list)String.join(", ", names)
Build a string from a numberString.valueOf(x)String.valueOf(42)
Parse a numberInteger.parseInt(s)throws if not numeric

C# — string

Want to...MethodExample
Get length.Length (property, no parens)s.Length
Get char at indexindexers[0]
Slice out a substring.Substring(start) / .Substring(start, length)s.Substring(1, 2) — second arg is length, not end index
Find a substring.IndexOf(sub) / .LastIndexOf(sub)s.IndexOf("lo")
Check if it contains.Contains(sub)s.Contains("lo")
Compare for equality== (overloaded for value equality) or .Equals(other)s == t is safe in C#, unlike Java
Split on a delimiter.Split(sep)s.Split(',')string[]
Trim whitespace.Trim()s.Trim()
Change case.ToUpper() / .ToLower()s.ToLower()
Replace.Replace(a, b)s.Replace("a", "b")
To a char array.ToCharArray()char[] cs = s.ToCharArray();
Join a list of stringsstring.Join(sep, list)string.Join(", ", names)
Check empty/nullstring.IsNullOrEmpty(s)guards against both at once
Parse a numberint.Parse(s) / int.TryParse(s, out x)TryParse avoids the exception

Python — str

Want to...MethodExample
Get lengthlen(s) (builtin, not a method)len(s)
Get char at indexindexings[0]
Slice out a substringslicings[1:3] (end-exclusive), s[::-1] reverses
Find a substring.find(sub) (-1 if missing) / .index(sub) (raises)s.find("lo")
Check if it containsin"lo" in s
Compare for equality==s == t
Split on a delimiter.split(sep)s.split(",")list[str]
Trim whitespace.strip()s.strip()
Change case.upper() / .lower()s.lower()
Replace.replace(a, b)s.replace("a", "b")
To a list of charslist(s)strings are already iterable
Join a list of stringssep.join(list)", ".join(names) — join is a string method, not a list method
Check type of content.isdigit() / .isalpha() / .isalnum()s.isdigit()
Parse a numberint(s)raises ValueError if not numeric

JavaScript / TypeScript — string

Want to...MethodExample
Get length.length (property, no parens)s.length
Get char at indexindexing or .charAt(i)s[0] or s.charAt(0)
Slice out a substring.slice(start, end)s.slice(1, 3) (end-exclusive; supports negative indices)
Find a substring.indexOf(sub) / .lastIndexOf(sub)s.indexOf("lo")
Check if it contains.includes(sub)s.includes("lo")
Compare for equality===s === t — always triple-equals
Split on a delimiter.split(sep)s.split(",")string[]
Trim whitespace.trim()s.trim()
Change case.toUpperCase() / .toLowerCase()s.toLowerCase()
Replace.replace(a, b) (first match) / .replaceAll(a, b)s.replaceAll("a", "b")
To an array of chars[...s] or s.split("")[...s] handles unicode better
Join an array of stringsarr.join(sep)names.join(", ") — join is an array method here, opposite of Python
Build from a numberString(x) or `${x}`template literals are idiomatic
Parse a numberNumber(s) / parseInt(s, 10)Number("") is 0, watch for that

The one gotcha that trips up almost everyone across languages: every "mutating-looking" call is not mutating — s.toUpperCase() doesn't change s, it returns a new string you have to capture. If your code compiles but the output looks unchanged, this is the first thing to check.

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

This question doesn't have a code exercise.