String Common Methods
What does length() method return?
Section titled “What does length() method return?”The length() method returns the total number of characters in the string as an integer, including spaces, special characters, and digits. It counts from 1 (not 0-indexed like arrays).
Interview Example:
String str = "Hello World";System.out.println(str.length()); // Output: 11String empty = "";System.out.println(empty.length()); // Output: 0String spaces = " ";System.out.println(spaces.length()); // Output: 3How do charAt(int index) and substring(int begin, int end) work?
Section titled “How do charAt(int index) and substring(int begin, int end) work?”charAt(index) returns a single character at the specified index (0-based). substring(begin, end) returns a new String from begin index (inclusive) to end index (exclusive).
Interview Example:
String str = "Interview";// charAt() - returns charchar ch = str.charAt(0); // 'I'char last = str.charAt(str.length() - 1); // 'w'// substring(begin, end) - returns StringString sub1 = str.substring(0, 5); // "Inter" (0 to 4)String sub2 = str.substring(5); // "view" (5 to end)String sub3 = str.substring(2, 7); // "tervi"// charAt throws StringIndexOutOfBoundsException// char invalid = str.charAt(20); // ExceptionWhat is the difference between substring() and subSequence()?
Section titled “What is the difference between substring() and subSequence()?”Both extract a portion of the string, but substring() returns a String object, while subSequence() returns a CharSequence interface. substring() is more commonly used.
Interview Example:
String str = "Java Programming";// substring() returns StringString sub = str.substring(5, 16);System.out.println(sub); // "Programming"// subSequence() returns CharSequenceCharSequence seq = str.subSequence(5, 16);System.out.println(seq); // "Programming"// substring() can use String methods directlySystem.out.println(sub.toUpperCase()); // Works// subSequence() needs casting for String methods// System.out.println(seq.toUpperCase()); // Compile errorSystem.out.println(((String)seq).toUpperCase()); // Needs castHow to check if a string contains another string?
Section titled “How to check if a string contains another string?”Use contains() method which returns boolean (true/false). It checks for exact substring match and is case-sensitive.
Interview Example:
String str = "Java is awesome";System.out.println(str.contains("Java")); // trueSystem.out.println(str.contains("awesome")); // trueSystem.out.println(str.contains("python")); // falseSystem.out.println(str.contains("java")); // false (case-sensitive)// Case-insensitive checkSystem.out.println(str.toLowerCase().contains("java")); // true// Alternative using indexOfif (str.indexOf("Java") != -1) { System.out.println("Found"); // Found}How to find the index of a substring using indexOf() and lastIndexOf()?
Section titled “How to find the index of a substring using indexOf() and lastIndexOf()?”indexOf() returns the first occurrence index of substring/character. lastIndexOf() returns the last occurrence index. Both return -1 if not found.
Interview Example:
String str = "Java Programming in Java";// indexOf() - first occurrenceSystem.out.println(str.indexOf("Java")); // 0System.out.println(str.indexOf("a")); // 1System.out.println(str.indexOf("Python")); // -1// lastIndexOf() - last occurrenceSystem.out.println(str.lastIndexOf("Java")); // 20System.out.println(str.lastIndexOf("a")); // 23// indexOf with start positionSystem.out.println(str.indexOf("Java", 5)); // 20 (search from index 5)// Check if substring existsif (str.indexOf("Java") != -1) { System.out.println("Java found");}How to convert a string to uppercase/lowercase?
Section titled “How to convert a string to uppercase/lowercase?”Use toUpperCase() to convert all characters to uppercase and toLowerCase() to convert to lowercase. Both return a new String (original unchanged).
Interview Example:
String str = "Java Interview";String upper = str.toUpperCase();System.out.println(upper); // "JAVA INTERVIEW"String lower = str.toLowerCase();System.out.println(lower); // "java interview"System.out.println(str); // "Java Interview" (original unchanged)// Case-insensitive comparisonString s1 = "JAVA";String s2 = "java";System.out.println(s1.toLowerCase().equals(s2.toLowerCase())); // true// Or use equalsIgnoreCase()System.out.println(s1.equalsIgnoreCase(s2)); // trueHow to trim spaces from start and end of a string using trim() and strip()?
Section titled “How to trim spaces from start and end of a string using trim() and strip()?”Both remove whitespace from start and end, but strip() (Java 11+) handles Unicode whitespace better. trim() only removes ASCII spaces (≤ U+0020).
Interview Example:
String str = " Hello World ";// trim() - removes ASCII whitespaceString trimmed = str.trim();System.out.println(trimmed); // "Hello World"// strip() - removes Unicode whitespace (Java 11+)String stripped = str.strip();System.out.println(stripped); // "Hello World"// stripLeading() - removes from start onlySystem.out.println(str.stripLeading()); // "Hello World "// stripTrailing() - removes from end onlySystem.out.println(str.stripTrailing()); // " Hello World"What’s the difference between trim() and strip()?
Section titled “What’s the difference between trim() and strip()?”trim() removes ASCII whitespace (char ≤ 32 or U+0020), while strip() removes all Unicode whitespace characters. strip() is more comprehensive and introduced in Java 11.
Interview Example:
String str1 = " Hello "; // Regular spacesString str2 = "\u2003Hello\u2003"; // Unicode em-space// trim() - handles only ASCIISystem.out.println(str1.trim()); // "Hello" ✓System.out.println(str2.trim()); // "\u2003Hello\u2003" ✗ (unchanged)// strip() - handles Unicode (Java 11+)System.out.println(str1.strip()); // "Hello" ✓System.out.println(str2.strip()); // "Hello" ✓// Use strip() for better Unicode support// Use trim() for backward compatibility with Java < 11How to replace a character or substring (replace(), replaceAll(), replaceFirst())?
Section titled “How to replace a character or substring (replace(), replaceAll(), replaceFirst())?”replace() replaces all exact matches (literal). replaceAll() uses regex to replace all matches. replaceFirst() uses regex to replace only the first match.
Interview Example:
String str = "Java is Java";// replace() - literal replacement, all occurrencesString r1 = str.replace("Java", "Python");System.out.println(r1); // "Python is Python"String r2 = str.replace('a', 'A');System.out.println(r2); // "JAvA is JAvA"// replaceAll() - regex replacement, all occurrencesString s = "Java123Programming456";String r3 = s.replaceAll("\\d+", "-");System.out.println(r3); // "Java-Programming-"// replaceFirst() - regex replacement, first occurrence onlyString r4 = s.replaceFirst("\\d+", "-");System.out.println(r4); // "Java-Programming456"How to split a string into an array (split() method)?
Section titled “How to split a string into an array (split() method)?”split(regex) divides a string into an array based on a delimiter (regex pattern). Returns String[] array. Can specify limit for maximum splits.
Interview Example:
String str = "Java,Python,C++,JavaScript";// Basic splitString[] langs = str.split(",");for (String lang : langs) { System.out.println(lang); // Java, Python, C++, JavaScript}// Split with limitString[] limited = str.split(",", 2);System.out.println(limited[0]); // "Java"System.out.println(limited[1]); // "Python,C++,JavaScript"// Split by whitespaceString text = "Hello World from Java";String[] words = text.split("\\s+");System.out.println(words.length); // 4// Split by multiple delimitersString data = "a,b;c:d";String[] parts = data.split("[,;:]"); // Output: ["a", "b", "c", "d"]How to join multiple strings (String.join(), StringJoiner())?
Section titled “How to join multiple strings (String.join(), StringJoiner())?”String.join() joins strings with a delimiter. StringJoiner provides more control with prefix/suffix options for building joined strings.
Interview Example:
// String.join() - simple and directString[] words = {"Java", "Python", "C++"};String joined = String.join(", ", words);System.out.println(joined); // "Java, Python, C++"// Join with varargsString result = String.join("-", "2025", "10", "21");System.out.println(result); // "2025-10-21"// StringJoiner - with prefix and suffixStringJoiner joiner = new StringJoiner(", ", "[", "]");joiner.add("Java").add("Python").add("C++");System.out.println(joiner.toString()); // "[Java, Python, C++]"// Using StreamsList<String> list = Arrays.asList("A", "B", "C");String s = list.stream().collect(Collectors.joining(", "));System.out.println(s); // "A, B, C"How to convert primitive or object values to string (String.valueOf(), toString())?
Section titled “How to convert primitive or object values to string (String.valueOf(), toString())?”String.valueOf() is a static method that converts any type to String (handles null safely). toString() is an instance method on objects (throws NullPointerException if null).
Interview Example:
// String.valueOf() - static method, null-safeint num = 100;String s1 = String.valueOf(num);System.out.println(s1); // "100"boolean flag = true;String s2 = String.valueOf(flag);System.out.println(s2); // "true"Object obj = null;String s3 = String.valueOf(obj);System.out.println(s3); // "null" (safe)// toString() - instance methodInteger i = 100;String s4 = i.toString();System.out.println(s4); // "100"Integer nullInt = null;// String s5 = nullInt.toString(); // NullPointerException// Concatenation (implicit valueOf)String s6 = "" + 100;System.out.println(s6); // "100"How to remove all spaces from a string?
Section titled “How to remove all spaces from a string?”Use replace(), replaceAll(), or modern replaceAll() with regex to remove all spaces (or specific whitespace characters).
Interview Example:
String str = "Java Programming Interview";// Method 1: replace() - removes only regular spacesString noSpaces1 = str.replace(" ", "");System.out.println(noSpaces1); // "JavaProgrammingInterview"// Method 2: replaceAll() - removes all whitespace (space, tab, newline)String text = "Java \t Programming \n Interview";String noSpaces2 = text.replaceAll("\\s+", "");System.out.println(noSpaces2); // "JavaProgrammingInterview"// Method 3: replaceAll() - removes only spacesString noSpaces3 = str.replaceAll(" ", "");System.out.println(noSpaces3); // "JavaProgrammingInterview"// To replace multiple spaces with single spaceString multiSpace = "Java Programming";String single = multiSpace.replaceAll("\\s+", " ");System.out.println(single); // "Java Programming"How to check if a string starts or ends with a given substring?
Section titled “How to check if a string starts or ends with a given substring?”Use startsWith() to check if string begins with a prefix and endsWith() to check if it ends with a suffix. Both return boolean and are case-sensitive.
Interview Example:
String fileName = "document.pdf";// startsWith()System.out.println(fileName.startsWith("doc")); // trueSystem.out.println(fileName.startsWith("Doc")); // false (case-sensitive)System.out.println(fileName.startsWith("pdf")); // false// endsWith()System.out.println(fileName.endsWith(".pdf")); // trueSystem.out.println(fileName.endsWith(".PDF")); // falseSystem.out.println(fileName.endsWith("ent.pdf")); // true// startsWith() with offsetString url = "https://example.com";System.out.println(url.startsWith("://", 5)); // true (starts at index 5)// Practical use case - file validationif (fileName.endsWith(".pdf") || fileName.endsWith(".doc")) { System.out.println("Valid document");}How to convert a string into a char[] and back?
Section titled “How to convert a string into a char[] and back?”Use toCharArray() to convert String to char array and String constructor or String.valueOf() to convert char array back to String.
Interview Example:
String str = "Java";// String to char[]char[] charArray = str.toCharArray();for (char c : charArray) { System.out.print(c + " "); // J a v a}// Modify char arraycharArray[0] = 'j';System.out.println(charArray); // java// char[] to String - Method 1: ConstructorString newStr1 = new String(charArray);System.out.println(newStr1); // "java"// char[] to String - Method 2: valueOf()String newStr2 = String.valueOf(charArray);System.out.println(newStr2); // "java"// Partial char[] to Stringchar[] arr = {'H', 'e', 'l', 'l', 'o'};String partial = new String(arr, 0, 3); // offset=0, count=3System.out.println(partial); // "Hel"// Reverse a string exampleString reverse = new String(new StringBuilder(str).reverse().toString().toCharArray());