String program

Here is a list of 10 interview-style Java programs for each of the 20 commonly used String methods. These can help you prepare effectively:


✅ 1. length()

  1. Check if a string is empty without using isEmpty().
  2. Count the number of characters in a sentence.
  3. Validate if input length is less than 10 characters.
  4. Compare lengths of two strings.
  5. Trim string and compare original vs trimmed length.
  6. Count total characters excluding spaces.
  7. Calculate total length of all words in a sentence.
  8. Validate password length (e.g., min 8 chars).
  9. Get length of each word in a string.
  10. Find average length of words in sentence.

✅ 2. charAt()

  1. Find first and last character of a string.
  2. Count occurrences of a specific character.
  3. Print characters at even positions.
  4. Replace character at given index.
  5. Reverse string using charAt().
  6. Count uppercase and lowercase letters.
  7. Check if all characters are digits.
  8. Print ASCII value of each character.
  9. Find vowels and their positions.
  10. Print alternate characters.

✅ 3. substring()

  1. Extract domain from an email address.
  2. Extract file extension from filename.
  3. Remove first and last character.
  4. Get initials from full name.
  5. Remove middle name from full name.
  6. Extract date from datetime string.
  7. Extract country code from phone number.
  8. Mask middle characters with '*'.
  9. Get substring between two symbols.
  10. Check if two substrings are equal.

✅ 4. indexOf()

  1. Find index of first vowel.
  2. Check if a word is present in sentence.
  3. Find position of “@” in email.
  4. Count how many times a substring appears (loop + indexOf).
  5. Find index of second occurrence.
  6. Check if character is at even index.
  7. Find position of space in sentence.
  8. Find index of special character.
  9. Find start index of domain name.
  10. Find position of duplicate characters.

✅ 5. lastIndexOf()

  1. Find index of last vowel.
  2. Find last space in sentence.
  3. Get last word using substring + lastIndexOf.
  4. Check if string ends with punctuation.
  5. Replace last character using last index.
  6. Find last position of specific substring.
  7. Count words after last comma.
  8. Check for repeated suffix.
  9. Get file name without extension.
  10. Get last digit position.

✅ 6. toLowerCase() / toUpperCase()

  1. Convert sentence to upper case.
  2. Count capital letters (before and after conversion).
  3. Convert camelCase to uppercase.
  4. Make search case-insensitive.
  5. Capitalize first letter of each word (mix with substring).
  6. Compare two strings case-insensitively.
  7. Convert mixed case sentence to lower.
  8. Convert user input to uppercase for validation.
  9. Encrypt simple text using upper case + shift.
  10. Convert first half lower, second half upper.

✅ 7. trim()

  1. Count actual characters excluding leading/trailing spaces.
  2. Compare user input before and after trimming.
  3. Validate trimmed input length.
  4. Remove space and check for empty input.
  5. Check if sentence starts with space.
  6. Trim and split input sentence.
  7. Compare trimmed and untrimmed strings.
  8. Print string after removing unwanted spaces.
  9. Read and trim form input fields.
  10. Count spaces before and after trimming.

✅ 8. equals() / equalsIgnoreCase()

  1. Compare two user-entered passwords.
  2. Validate login with exact match.
  3. Check if two words are same (case-insensitive).
  4. Check if word equals "exit" to terminate loop.
  5. Compare strings after trimming.
  6. Compare uppercase converted string with original.
  7. Validate user roles (admin, user).
  8. Match strings read from config file.
  9. Case-insensitive search in array of strings.
  10. Match user input with predefined values.

✅ 9. contains()

  1. Check if string contains a substring.
  2. Search keyword in sentence.
  3. Validate email contains "@gmail.com".
  4. Check if password contains number or special char.
  5. Find substring using contains + substring.
  6. Validate presence of domain in URL.
  7. Check if file path contains folder name.
  8. Count matches using loop and contains.
  9. Verify if string contains digit using loop + contains.
  10. Simple filter using contains in array.

✅ 10. startsWith() / endsWith()

  1. Validate if string starts with "https".
  2. Check if file ends with .txt.
  3. Check if name starts with capital letter.
  4. Filter strings starting with specific prefix.
  5. Check if string ends with punctuation mark.
  6. Verify phone number starts with "+91".
  7. Validate email ends with domain.
  8. Count strings that start with "test".
  9. Match format like "INV-2023-001".
  10. Check if path ends with slash.

✅ 11. replace(char old, char new)


1. Replace all spaces with underscores.



2. Mask password with * (replace every char).



3. Replace all vowels with #.



4. Remove all digits (replace with "").



5. Replace commas with tabs in CSV line.



6. Replace all occurrences of a with @.



7. Normalize phone number format.



8. Convert sentence to leetspeak (e.g., a→4, e→3).



9. Replace capital letters with small (manually).



10. Clean text: replace all special chars with _.





---


✅ 12. replaceAll(String regex, String replacement)


1. Remove all digits using \\d.



2. Remove all whitespaces using \\s.



3. Replace multiple spaces with single space.



4. Mask all characters except last 4 digits in credit card.



5. Replace all non-alphabetic characters.



6. Keep only numbers from a string.



7. Replace all lowercase with uppercase using regex groups.



8. Replace all special symbols in password.



9. Remove all letters from string.



10. Replace all newline characters with <br>.





---


✅ 13. split(String regex)


1. Split sentence into words.



2. Parse comma-separated values.



3. Tokenize email by @ and ..



4. Split file path by / or \\.



5. Separate date by - or /.



6. Split string by multiple spaces.



7. Separate sentence into individual characters.



8. Parse key-value pairs key=value&key2=value2.



9. Split sentence and reverse words.



10. Split Java package name into parts.





---


✅ 14. isEmpty() / isBlank() (Java 11+)


1. Check if user input is empty.



2. Validate field not blank after trim.



3. Filter out empty strings from array.



4. Loop through array and skip blank entries.



5. Count blank lines in a multiline input.



6. Validate text area input before submission.



7. Skip blank comments in logs.



8. Validate email is not blank.



9. Check blank input using isBlank() (Java 11+).



10. Remove all blank strings from list.





---


✅ 15. concat(String)


1. Concatenate first name and last name.



2. Add extension to filename.



3. Join words from array into sentence.



4. Create full address from parts.



5. Concatenate strings in loop.



6. Add protocol to URL (http + domain).



7. Append symbols around text.



8. Build message using loop + concat.



9. Join list of numbers into string.



10. Append prefix/suffix to string.





---


✅ 16. compareTo(String) / compareToIgnoreCase()


1. Sort array of strings alphabetically.



2. Compare two strings lexicographically.



3. Find if two strings are equal (returns 0).



4. Compare two strings ignoring case.



5. Find smaller or greater string.



6. Sort names in ascending order.



7. Compare version numbers (basic).



8. Validate order of input strings.



9. Check if string comes before another.



10. Custom sort using compareTo.





---


✅ 17. toCharArray()


1. Convert string to char array and print all chars.



2. Count frequency of each character.



3. Reverse string manually.



4. Find duplicate characters.



5. Sort characters in string.



6. Remove duplicate characters using set.



7. Check if all characters are digits.



8. Check if string has all vowels.



9. Replace specific characters using array.



10. Count special characters using loop.





---


✅ 18. valueOf()


1. Convert int to string.



2. Convert boolean to string for display.



3. Join string + number using valueOf.



4. Convert float to string and print.



5. Convert char array to string.



6. Format result using string + valueOf.



7. Append integer to string using valueOf().



8. Store numerical result in string variable.



9. Create full date string from parts.



10. Print object value using valueOf.





---


✅ 19. startsWith() + Logic (again in mixed usage)


1. Check if ID starts with “EMP”.



2. Validate filename starts with “IMG”.



3. Check if name starts with capital.



4. Detect prefix in list of strings.



5. Filter strings that start with http.



6. Group data based on initial letter.



7. Detect if log starts with “ERROR”.



8. Filter items from array by prefix.



9. Count words that start with vowel.



10. Find common prefix in multiple strings.





---


✅ 20. endsWith() + Logic (again in mixed usage)


1. Check if email ends with .com.



2. Filter file names ending with .txt.



3. Check if message ends with punctuation.



4. Detect phone number ending with specific digits.



5. Count how many strings end with s.



6. Match image files ending with .jpg, .png.



7. Find suffix in input.



8. Replace ending based on suffix.



9. Validate URL ends with slash.



10. Create report file ending with .csv.


Comments

Popular posts from this blog

10 automation test cases for https://www.saucedemo.com/ (Sauce Demo)

Java Roadmap for SDET Superstars!

Mastering XPath in Selenium 4 ๐Ÿš€ – Supported Functions & Axes Explained