Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program to show an example of extracting subStrings from a String

Posted By: Adalhard Fischer     Category: Java     Views: 3697

A Java Program to show an example of extracting subStrings from a String.

Code for Program to show an example of extracting subStrings from a String in Java

publicclass JAVA_052
 {
    publicstaticvoid main(String[] args)
    {
       String Text="To be or not to be";

       System.out.println("The String is : " + Text + "\n");

       int count=0;
       int index=0;

       char Separator=' ';

       do
       {
          ++count;
          ++index;

          index=Text.indexOf(Separator,index);
       }
       while(index!=-1);

       String[] subStr=new String[count];

       int endIndex=0;

       index=0;

       for(int i=0;i<count;i++)
       {
          endIndex=Text.indexOf(Separator,index);

          if(endIndex==-1)
             subStr[i]=Text.substring(index);

          else
             subStr[i]=Text.substring(index,endIndex);

          index=(endIndex+1);
       }

       System.out.println("The extracted subStrings are :");

       for(int j=0;j<count;j++)
          System.out.println(subStr[j]);
    }
 }
  
Share: 



Adalhard Fischer
Adalhard Fischer author of Program to show an example of extracting subStrings from a String is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!