Top 75+ String Programs In Java

Top 75+ String Programs In Java | Similar to Array Programs in Java, we have listed String Programs in Java. It can help you with a basic understanding of the string in Java programming language. In these programs, we have covered string handling questions, number programs using string, string array programs, and string programs with the help of collections.

In these programs, String class methods will provide a lot of help while developing the programs. You don’t have to remember each and every method, just know their name & uses, and wherever there is a need see the examples on the method as references.

List of String Programs in Java

  1. Take String Input using Scanner
  2. How to Find Length of String in Java
  3. Length of String Without length()
  4. How to Iterate through String Java
  5. Java For Each Character In String
  6. Sum of digits in a String
  7. Count No of Vowels String
  8. String Pattern Programs in Java
  9. How To Reverse a String In Java
  10. String Palindrome In Java
  11. Sort String In Java
  12. How to Compare Strings In Java
  13. Check If Char is Uppercase
  14. Check If String is Uppercase Java
  15. String Contains Uppercase or Lowercase
  16. Swap Characters in String Java
  17. Generate Random Strings
  18. Java Program to Find Weight of String
  19. Find the Second Occurrence of Character
  20. Find the Last Occurrence of Character
  21. Split String By Line in Java
  22. Compare Strings Alphabetically Java
  23. Put Quotes in a String Java
  24. Java String Parsestring
  25. How to Check if String is Empty Java
  26. How to Compare String to Enum Java
  27. How to Concat Int to String in Java
  28. New Line In Java String
  29. Find Repeating Pattern In String Java
  30. Java Set Character in String
  31. Appending String In Java
  32. Concat Strings in Java
  33. Java String Interpolation

String Programs in Java to Replace or Remove Character or Substrings

  1. Remove Character From a String Java
  2. Remove Substring From String
  3. Replace Comma in String Java
  4. Remove Commas From String Java
  5. Replace Special Characters In Java
  6. Remove Spaces From String In Java
  7. Remove Special Characters From String
  8. Remove Carriage Return of String
  9. How to Replace Dot in Java
  10. Replace Last Occurrence of Character
  11. Java Replace nth Occurrence of String
  12. Replace String With Escape Character

Conversion of/to String Programs in Java

  1. Convert String to Int in Java
  2. How To Convert Int To String In Java
  3. Transform String To Int Java & Vice-Versa
  4. java.lang.String Cannot Be Converted to Int
  5. Convert String to Enum Java
  6. Deserialize String To Enum Java
  7. Java Long to String
  8. Convert String to ASCII Java
  9. String To Char Array Java
  10. Char Array To String Java
  11. String To StringBuilder Java
  12. StringBuilder To String Java
  13. Java String To StringBuffer
  14. StringBuffer To String In Java

String Array Programs

  1. Java Convert Number String to Array
  2. How to Iterate Through a String Array In Java?
  3. Convert String Array to Lowercase Java
  4. String Array to UpperCase Java
  5. How to Sort a String Array Lexicographically in Java
  6. Converting a String Array To Int Array in Java
  7. Find The Longest String In An Array Java
  8. Bubble Sort Char Array Java

Number programs using String

  1. Unique Number
  2. Fascinating Number
  3. ISBN Number

Others/Games Programs using String

  1. Hidden Word Java Program
  2. Java Secret Message Program

String Programs with the help of Collection

  1. Print Vowels & Consonants in String
  2. Convert Comma Separated String to List Java
  3. Convert Pipe Delimited String to List Java
  4. How To Find Repeated Characters In A String In Java
  5. Cast Object to List String in Java
  6. Java Convert Object List To String List
  7. Java Convert String List To Array
  8. Java String Array to String List
  9. Convert Map to String Java
  10. Convert String to Map Java
  11. Join List Of Strings in Java

String Programs In Java with Examples

1) Take String Input In Java using Scanner Class:- Develop a Java program to take string input, store it in a variable and display it.

Enter Name: Know Program
Entered value: Know Program


2) Find Length of String in Java:- Develop a Java program to find the length of the string. Tip:- You can take the help of the length() method of the String class.

String: Know Program
Length of given string: 12


3) Find Length of String in Java Without Using length() Method:- Write string programs in Java to find the length of a string without taking the help of the string class length() method.

String: Hello, World!
Length of given string: 13


4) Iterate through String in Java:- Develop a string program in Java to iterate the characters of the strings. Take a string, iterate each character and display them on the screen.

String: Java
Characters of the string:
J
a
v
a


5) Find the Sum of Digits in a String in Java:- Write a string program in Java to find the sum of digits in the given string. Take a string, find the sum of digits and display them.

Enter String: hello2021
The sum of digits in the string “hello2021” = 5

Enter String: Sophia@1999
The sum of digits in the string “Sophia@1999” = 28


6) Count the Number of Vowels in a String in Java:- Develop a Java program to count the number of vowels in a string. Take a string, iterate through the characters of the string and check whether it is a vowel or not. If it is a vowel then increase the count value.

Enter String: KnowProgram@2025
Number of vowels: 3

Enter String: Hello, How are you?
Number of vowels: 7


7) String Pattern Programs in Java:- Develop string programs in Java for the given patterns. Take a string as input and display the pattern.

Enter a word: PHYSICS
   P
   H
   Y
PHYSICS
   I
   C
   S

Enter a word: PRO
P
PR
PRO

Enter a word: PRO
P
PR
PRO
PR
P

Enter a word: PROGRAM
      P
     PR
    PRO
   PROG
  PROGR
 PROGRA
PROGRAM
 ROGRAM
  OGRAM
   GRAM
    RAM
     AM
      M
Enter a word: PROGRAM
Enter number of lines: 6
       P 
      R O 
     G R A 
    M P R O 
   G R A M P 
  R O G R A M 
 P R O G R A M 

8) Reverse A String In Java:- Write a string program in Java to reverse the given string. Take a string and reverse it.

String: Know Program
String after reverse: margorP wonK

Also, develop a Java program to reverse each word in the given string as below:-

String: Java Programming Language
String after reverse: avaJ gnimmargorP egaugnaL

And develop a program to reverse a sentence in Java as shown below. Just reverse the order of words without reversing the characters. The first word of the string should come last and the last word should come at the first place.

Enter a sentence:
I am learning Java programming language.
Reversed sentence:
language. programming Java learning am I


9) String Palindrome In Java:- Develop a string program in Java to check whether the given string is a palindrome or not.

Enter a string: madam
Palindrome

And for a given string find the longest palindrome available as a substring:-

Enter a string: bananas
Longest Palindrome string: anana


10) Sort String In Java:- Develop a Java program to sort the characters of a given string. The ASCII value of the uppercase character is lesser than the lowercase character therefore uppercase should come first.

Given String: Know Program
Sorted String: KPagmnoorrw

Also develop string programs in Java to sort an array of strings in ascending and descending order as shown below:-

String Array: [program, website, know]
Ascending Array: [know, program, website]
Descending Array: [website, program, know]


11) Compare Strings In Java:- Write a Java program to compare two given strings. Check whether both are alphabetically equal or not. If they are not equal then find the difference between them.


12) Check If Char is Uppercase:- Write a Java program to check whether the given character is in uppercase or not?

Character: A
UPPERCASE

Character: a
NOT UPPERCASE


13) Check If String is Uppercase:- Write a Java program to check whether the given string is in uppercase or not?

Enter String: KnowProgram
Not Uppercase


14) Check If String Contains Uppercase Or Lowercase:- Develop string programs in Java to check whether the given string contains only uppercase or lowercase characters?

String: KNOW PROFRAM
Uppercase

String: know program
Lowercase


15) Swap Given Characters of String in Java:- Write string programs in Java to swap two given characters of a string in Java programming language.

Know Program
Know Paogrrm


16) Generate Random Strings in Java:- Write a Java program to generate random strings in Java. Random strings can contain uppercase, lowercase, numbers, and special characters.

Random string using randomUUID(): 409962da-daeb-4685-87b9-47db2354e266

Random String: CSHDDVOIU

Random String: 79LmFdFKOL

Random Base64 String is: Q3diKGRMaG8zPlldT2JHLg==


17) Find the Weight of the String:- Write a Java program to find the weight of the string based on the given rules.

String: Know Program
Weight of string ignoring vowels = 120
Weight of string including vowels = 151


18) Find Second Occurrence of Character in String Java:- Write string programs in Java to find the second occurrence of a given character in the given string. If it exists then display the index.

String: “Know Program”
Char: ‘o’
Result: 7


19) Find the Last Occurrence of Character in String Java:- Write a Java program to find the index of the last occurrence of the given character of the string.

String: “Java Programming”
Char: ‘g’
Result: 15


20) Split String By Line in Java:- Write a Java program to split the given string by line. The string contains lines, you have to split it based on the line and display the result.

String: “Hi.\nHow are you?\nAre you eating?”
Result (array of string): [Hi., How are you?, Are you eating?]


21) Compare Strings Alphabetically:- Write a Java program to compare two strings alphabetically. Check whether they are alphabetically similar or not.


22) Put Quotes around a String:- Develop string programs in Java to put quotes around or inside the string.

“Java Programming Language”
‘Java Programming Language’


23) Check if String is Empty Java:- Write string programs in Java to check whether the string is empty or not. You can consider the cases ignoring the whitespaces.


24) Compare String to Enum Java:- Write a Java program to compare a given string to the given Enum. If Enum contains that string then they are equal else they are not.

Enum: {ONE, TWO, THREE, FOUR};
String: “TWO”
Output = equal


25) Concat Int to String in Java:- Develop a program to concat an int value to the string. Take a string, take a int value, append string with int.

String = “ABC”
int = 123
After concatenation:- “ABC123”


26) Set Character in String:- Write string programs in Java to set character at beginning, end or at the given position of the string.

String = “java”
Character to add to the end of string = ‘c’
Resultant string = “javac”

String = “now”
Character to add to the start of string = ‘k’
Resultant string = “know”

String = “clan”
Character to add = ‘e’
Position = 2
Resultant String = “clean”


27) Place New Line In String:- Develop string programs in Java to place the new line in a given string. You can use newline characters or pre-defined methods for this purpose.


28) Find Repeating Pattern In String:- Develop string programs in Java to find the repeating pattern in a string.

String1: “acbdfghybdf”
String2: “acbdfghybdfghabcd”;
Max length repeating pattern in string1: bdf
Max length repeating pattern in string2: bdfgh


29) Remove Character From a String Java:- Write string programs in Java to remove the first or last character from the given string. Or remove a character from the given position of the string.


30) Remove Substring From String Java:- Develop a string program in Java a substring from the given string. If the substring doesn’t exist in the given string then it should not perform the remove operation.

String: “Know Program – Java Programming”
String after removing “Program” substring from the given string: “Know – Java ming”


31) Replace Last Occurrence of Character in String:- Write a Java program to replace the last occurrence of the given character with a new character from the string.

String: Program
Character to be replaced: r
New character to place: X
After replacing the last occurrence of ‘r’ with ‘X’ = ProgXam


32) Replace nth Occurrence of String:- Develop a Java program to replace the nth occurrence of the given substring with another substring from the given string.

String: good morning, good afternoon, good evening goodnight
String to be replaced: good
New string to place: Hello
Occurance: 3
Resultant String: good morning, good afternoon, Hello evening goodnight


33) Remove Spaces From String In Java:- Write a Java program to remove spaces from the string. The spaces can be at the beginning, end, or middle of the string.


Leave a Comment

Your email address will not be published. Required fields are marked *