Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java String Substring Example

  1. /*
  2. Java substring example.
  3. This Java substring example describes how substring method of
  4. Java String class can be used to get substring of the given Java string object.
  5. */
  6.  
  7. public class JavaSubstringExample{
  8.    
  9.     public static void main(String args[]){
  10.        
  11.         /*        
  12.         Java String class defines two methods to get substring from the given
  13.         Java String object.
  14.        
  15.         1) public String substring(int startIndex)        
  16.         This method returns new String object containing the substring
  17.         of the given string from specified startIndex (inclusive).
  18.        
  19.         IMPORTANT: This method can throw IndexOutOfBoundException if
  20.         startIndex is negative or grater than length of the string.
  21.        
  22.         2) public String substring(int startIndex,int endIndex)        
  23.         This method returns new String object containing the substring of the
  24.         given string from specified startIndex to endIndex.
  25.        
  26.         Here, startIndex is inclusive while endIndex is exclusive.
  27.        
  28.         IMPORTANT: This method can throw IndexOutOfBoundException
  29.         if startIndex is negative and if startIndex of endIndex is
  30.         grater than the string length.
  31.        
  32.         */
  33.        
  34.         String name="Hello World";
  35.        
  36.         //This will print the substring starting from index 6        
  37.         System.out.println(name.substring(6));
  38.        
  39.         /*
  40.         This will print the substring starting from index 0 up to 4 not 5.
  41.         IMPORTANT: Here startIndex is inclusive while endIndex is exclusive.
  42.         */
  43.        
  44.         System.out.println(name.substring(0,5));
  45.        
  46.     }
  47.  
  48. }
  49.  
  50. /*
  51. OUTPUT of the above given Java substring Example would be:
  52. World
  53. Hello
  54. */
Your rating: None Average: 4.3 (11 votes)
Share this
Anonymous's picture

the second println would

the second println would actually ouput: Hello_ (There would not be an underscore, but a space.)
Anonymous's picture

I do not agree, it will just

I do not agree, it will just print "hello" as given in the example. Remember, start index is inclusive, but end index is exclusive (means will not be included). So start from index 0 to 4 will be assigned to substring and hence it will be "hello" ('h' at index 0 to 'o' at index 4).
Anonymous's picture

Hi ... if we given as

Hi ... if we given as name.substring(0,15) then what would be the result??? will you give me result... plz
Anonymous's picture

As described in the example

As described in the example itself, it will throw IndexOutOfBoundException as 15 is grater than string length.
Anonymous's picture

how can manipulate a

how can manipulate a substring in function
Anonymous's picture

If for example you had to

If for example you had to create a user id from a mixture of their initial and 5 letters from their surname - Ie jblogg how could you make the program so it could still work if someone only had a 3 letter surname?
Anonymous's picture

How to find substring of a

How to find substring of a string in java without using substring() function.....???
Anonymous's picture

How to find string length in

How to find string length in Java without using stringlength method? Use of substring and charAt are allowed.
Anonymous's picture

In for loop form start index

In for loop form start index to scan until null or end of the sting and increase count varable from 0 onwords.
Anonymous's picture

if there a large number of

if there a large number of datas means how can i findout the index properly
Anonymous's picture

.

.
Anonymous's picture

nirthi podoo

nirthi podoo

Post new comment