A String in Java is a sequence of characters that can be used to store and manipulate text data and It is basically an array of characters that are stored in a sequence of memory locations. All the strings in Java are immutable in nature i.e. once the string is created we can’t change it. This article provides a variety of programs on strings, that are frequently asked in the technical round in various software engineering interviews including various operations such as reversing, Iteration, palindrome, swapping, and splitting of strings etc.
Here, you practice all basic to advanced programs related to strings and each program comes with a detailed description, Java code, and output. All of the examples have been thoroughly tested on both Windows and Linux systems.
What is String in Java?
A String is an object of the String class, which is part of the java.lang package. A String can be created by using the String literal:
syntax:
String name = "John";
By using the new keyword and the constructor of the String class:
Syntax:
String name = new String("John");
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.
Java String Programs for Practice: Complete List
Here is a complete list of Java String practice programs divided into basic and advance category :
Java String Programs: Basic Strings Questions with Solutions
- Java String Program to Print even length words
- Java String Program to Insert a string into another string
- Java String program to check whether a string is a Palindrome
- Java String Program to Check Anagram
- Java String Program to Reverse a String
- Java String Program to Print a New Line in String
- Java String Program to Add Characters to a String
- Java String Program to Iterate Over Characters in String
- Java String Program to Convert Enum to String
- Java String Program to Get a Character From the Given String
- Java String Program to Convert String to String Array
- Java String Program to Swapping Pair of Characters
- Java String Program to Splitting into a number of sub-strings
Java String Programs: Advance Strings Questions with Solutions
- Java String Program to Replace a Character at a Specific Index
- Java String Program to Remove leading zeros
- Java String Program to Reverse a String Using Stacks
- Java String Program to Sort a String
- Java String Program to Convert String to InputStream
- Java String Program to Check Whether the Given String is Pangram
- Java String Program to Print the first letter of each word using regex
- Java String Program to Determine the Unicode Code Point at a given index
- Java String Program to Compare two strings
- Java String Program to Compare two strings lexicographically
Conclusion
In Conclusion, By practising these Java String questions you will get enough confidence to face any string questions in your upcoming Interview.
Also, feel free to check out our Java interview questions collection – it could come in handy!
Java Strings Programs – FAQs
1. What is string with example?
Strings are used for storing text/characters in a contiguous memory locations. For example, “Geeks for Geeks” is a string of characters.
2. What is insert () in Java?
insert() method of the Java StringBuffer class is used to insert the given string at the specified index. There are various overloaded methods of insert().
3. How to add strings in Java?
In Java, you can combine strings using +, +=, or the concat() method. This is called concatenation.
4. What are the 5 string methods?
Method |
Description |
---|---|
indexOf() |
Returns the index within the string of the first occurrence of a specified character or substring. |
toCharArray() |
Converts the string to a character array. |
charAt() |
Returns the character at the specified index. |
concat() |
Concatenates one or more strings to the original string. |
replace() |
Replaces occurrences of a specified character or substring. |