Java String Programs
Last Updated :
22 Jun, 2024
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 Programs: Advance Strings Questions with Solutions
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. |
Please Login to comment...