Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Software Developor, Java Beginners SubstringExample

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

;
 
Java Examples for Beginners
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

Below is the class file containing all the substrings method that can be used in Java......
Have fun

////////////////

package utility;
import java.util.*;

//Author - Akshay Jangid

public class Substrings {
public static String SubstringAfter(String source, String value)
{
if (value.isEmpty())
{
return source;
}
//CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = source.indexOf(value);
if (index < 0)
{
//No such substring
return "";
}
return source.substring(index + value.length());
}

public static String SubstringBefore(String source, String value)
{
if (value.isEmpty())
{
return value;
}

int index = source.indexOf(value);
if (index < 0)
{
//No such substring
return "";
}
return source.substring(0, index);
}
public static String SubstringAfterLast(String source, String value)
{
if (value.isEmpty())
{
return source;
}

int index = source.lastIndexOf(value);
if (index < 0)
{
//No such substring
return "";
}
return source.substring(index + value.length());
}

public static String SubstringBeforeLast(String source, String value)
{
if (value.isEmpty())
{
return value;
}

int index = source.lastIndexOf(value);
if (index < 0)
{
//No such substring
return "";
}
return source.substring(0, index);
}

public static String SubstringBetween(String source,String string1, String string2)
{
int pos1=source.indexOf(string1);

int pos2=source.indexOf(string2);


if(pos1!=-1 && pos2!=-1)
{
if(pos1>pos2)
{
pos2=source.indexOf(string2,source.indexOf(string1));
if(pos2!=-1)
pos1=source.lastIndexOf(string1,pos2);

}
if(pos2!=-1 && pos1!=-1)
return source.substring(pos1+string1.length(),pos2);
else
return "";

}
else return "";
}


public static String SubstringBetweenLast(String source,String string1, String string2)
{
int pos1=source.lastIndexOf(string1);

int pos2=source.lastIndexOf(string2);


if(pos1!=-1 && pos2!=-1)
{
if(pos1>pos2)
{
pos1=source.lastIndexOf(string1,source.lastIndexOf(string2));
if(pos1!=-1)
pos2=source.indexOf(string2,pos1);

}
if(pos1!=-1 && pos2!=-1)
{

return source.substring(pos1+string1.length(),pos2);
}
else
return "";

}
else return "";
}



public static String[] SubstringBetweenAll(String source,String string1, String string2)
{ int p1;
Vector<String> vec = new Vector<String>();
String temp=source;
for(int i=0;i<source.length();i++)
{
if(temp=="")
break;
if(SubstringBetween(temp,string1,string2)!="" && SubstringBetween(temp,string1,string2).trim().length()!=0)
vec.add(SubstringBetween(temp,string1,string2));

if((SubstringBetween(temp,string1,string2))=="")
{
p1=temp.indexOf(string2,temp.indexOf(string1));
if (p1!=-1)
temp=temp.substring(p1+1,temp.length());
else
break;
}
else
temp=SubstringAfter(temp,SubstringBetween(temp,string1,string2));


}
String[] criteria = new String[vec.size()];

return vec.toArray(criteria);

}
}







Posted by Akshay Jangid on Tuesday, 06.15.10 @ 00:59am | #97580

Ask programming questions?

 

 

Back to Tutorial
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.