OOPs Concepts

Language Elements

Exceptions

Java String Class

Java Array Class

Collections

Java Example Codes

Core Java Interview Questions

Java String Class


There are two common ways of declaring string in Java i.e. by "string literal" and by using "new key word".

String str = "abc";

An object is created here and stored in String pool area of memory.

String str = new String("abc");

Here string is stored as an object in heap area of memory. The variable string will refer to the object in the heap memory.

String is mutable in Java. StringBuffer and StringBuilder can be used to create mutable strings. String and StringBuffer is also synchronized (thread safe), while StringBuilder is non-synchronized.

Another important class is StringTokenizer. It is a class to split a String into different pieces, each called as "token", as defined by delimiter, and stored in StringTokenizer object from which they can be retrieved. StringTokenizer object for a string str can be defined as :

StringTokenizer stringObject = new StringTokenizer(str, delimiter);

Delimiter can be . or : etc. Multiple delimiters can also be used at the same time. In the absence of any delimiter provided, white space will be used. Multiple delimiters can break String into tokens at same time. In the absence of any delimiter provided, by default it will use white-space.

String can also be tokenized by using split method, which on many instances is considered a better approach. The split() method is used to split a given string around matches of the given regular expression. One of the main advantage over legacy class StringTokenizer is that it supports regular expression.


Commonly used String class methods

  •   + operator - combine the two strings when placed in between.

  •   String concat(str) - attach two strings, by joining the str string with first string.

  •   char charAt(int index) - returns the character at the specified index.

  •   String substring(int start) - return sub string starting from character start.

  •   String substring(int start, int end) - return sub string starting from character start and ending at character end.

  •   int length() - return the total number of characters in a string.

  •   String toLowerCase() - change all the characters to lower case.

  •   String toUpperCase() - change all the characters to upper case.

  •   String trim() - eliminates the leading and trailing empty spaces.

  •   int indexOf(int chr) - return the index of first occurrence of specified character "chr" in the given string.

  •   int Integer.parseInt("123") - change the string given string to integer values 123. Integer.valueOf("123") can also be used.

  •   String String.toString( int 123) - change the int 123 from integer to string type. String.valueOf(int 123) can also be used.

Java String Questions


  •  What is difference between using == and equals() for compairing two strings in Java?

  •  How to check if given string is Palindrome?

  •  How to reverse a String in Java, with and without using any of the Java built in method?

  •  Split up to three strings in between the dash characters for string "split-this-into-maximum-three-strings"?

  •  Break and returns a character array containing individual characters for the string "www.zyasin.com"?

  •  Write a Java Program to Swap first and tenth character of words in string "java tutorial at www.zyasin.com"?

  •  Find all the characters repeated at least twice in a given string? Print "no duplicate" if there is none?

  •  Truncate the white spaces, if any, before and at the end of a string?

  •  Create a substring, starting at fifth and ending at tenth character of the string "tutorials at www.zyasin.com"?

  •   Display all the characters at the even indexes for the string "tutorials at www.zyasin.com"?

  •   Count how many times, the charcater "w" is present in the string "tutorials at www.zyasin.com"?

  •   Throw " StringIndexOutOfBoundsException" exception by trying to print a charcater longer than the length of string, say at 30th index of "tutorials at www.zyasin.com"?





Copyright © by Zafar Yasin. All rights reserved.