String Handling

On Social Media

String handling in Java is an essential part of programming, as strings are used extensively in applications. Java provides the String class and various methods to handle and manipulate strings efficiently. As string is probably the most commonly used class in java library. String class is encapsulated under java.lang package. In java, every string that you create is actually an object of type String. One important thing to notice about string object is that string objects are immutable that means once a string object is created it cannot be altered.

Creating a String object

String object and how they are stored

Creating String in heap

Concatenating String

String Comparison

String class function

indexOf() method has four forms:

  • int indexOf(String str) : It returns the index within this string of the first occurrence of the specified substring.
  • int indexOf(int ch, int fromIndex) : It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
  • int indexOf(int ch) : It returns the index within this string of the first occurrence of the specified character.
  • int indexOf(String str, int fromIndex) : It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
  • public String substring(int begin) : It represents the starting point of the subtring. If the substring() method is
    called with only one argument, the subtring returned, will contain characters from specified
    starting point to the end of original string.
  • public String substring(int begin, int end) : If the substring() method has two arguments, the second argument specify the end point of substring. Hence, this method is used to return a new String object that includes a substring of the given string with their indexes lying between start point and end point.

StringBuffer class

  • StringBuffer ( ) : creates an empty string buffer and reserves room for 16 characters.
  • StringBuffer ( int size ) : creates an empty string and takes an integer argument to set capacity of the buffer.
  • StringBuffer ( String str ) : Constructs a string buffer initialized to the contents of the specified string.
  • StringBuffer ( charSequence [ ]ch ) : Constructs a string buffer that contains the same characters as the specified CharSequence.

StringTokenizer Class


On Social Media

Leave a Reply

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