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!

Java Beginners SubstringExample

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

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

Current Comments

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

please assist with the code. when i test it it doesnt return the substring. what could be the problem?
String F32 = sourceMsg.getString("ValueDateC");

if (F32.length() == 0)
{
F32 = sourceMsg.getString("ValueDateD");
}

if (F32.length() >= 5)
{
F32.substring(5);
}

targetMsg.setField("Code", F32);

TEST
public static void main(String[] args)
{
String F32 = "100115EUR177,5";

System.out.println("F32BF= " + F32);

if (F32.length() >= 5)
{
F32.substring(4);

}

System.out.println("F32AF= " + F32);


}

Posted by KB on Wednesday, 09.15.10 @ 17:36pm | #99604

hemm I would like to ask.
If given a string 0.00054
then
substring(0,1)
the result will be 5, not 0.
Why does it happen?
thanks

Posted by angel on Saturday, 06.26.10 @ 11:46am | #97796

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

Hi Rose India Team,

Please send me program code
i'll be very much thank full.
Problem like this
I need to write java program(servlet)that serch a particular string on the web page.
There is a servlet in that servlet we hard code the url like we hard code http://www.roseindia.net/ now there is some hard code string like "Employee" , now the servlet will open that page roseindia.net , after that it search for string "employee" if string "employee" is available then it show result in another servlet that string found , other wise show message string not found.
please send me hint or source code asap , i'll be very much thank full,as it is very much necessary for me.

Thank you very much.
Regards,
Pradeep

Posted by Pradeep on Saturday, 08.29.09 @ 22:09pm | #90420

Hi Rose India Team,

Please send me program code
i'll be very much thank full.
Problem like this
I need to write java program(servlet)that serch a particular string on the web page.
There is a servlet in that servlet we hard code the url like we hard code http://www.roseindia.net/ now there is some hard code string like "Employee" , now the servlet will open that page roseindia.net , after that it search for string "employee" if string "employee" is available then it show result in another servlet that string found , other wise show message string not found.
please send me hint or source code asap , i'll be very much thank full,as it is very much necessary for me.

Thank you very much.
Regards,
Pradeep

Posted by Pradeep on Saturday, 08.29.09 @ 22:06pm | #90419

can you put some easy sample of Java, like making code for Hello World.

Posted by Dina on Monday, 07.27.09 @ 12:59pm | #89613

hello, only I like give them my congratulations, I am sorry I am not speak english.

Posted by ignacio on Thursday, 02.5.09 @ 12:50pm | #84534

You have given wrong explanation see javadocs


Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

Examples:

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)



public String substring(int beginIndex,
int endIndex)

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

Posted by tanay on Tuesday, 08.19.08 @ 17:18pm | #73771


I need help for get only the filename from attachment url D:\MDM\get_url.txt it is not default.this url is change everytime attachment.please help me how to get filename without txt.

Posted by poongothai on Wednesday, 07.23.08 @ 11:34am | #68563

thanking you vry much.... it helped me a lot

Posted by sunil on Friday, 06.13.08 @ 16:56pm | #63216

Amazing example....very clear...gud work...

Posted by Sathish on Thursday, 03.13.08 @ 21:34pm | #52620

Really a good example.

Posted by Abhishek on Monday, 01.14.08 @ 11:58am | #45163

How would done like one dimainsonal array and two diaminsonal.

Posted by ujjal on Monday, 08.13.07 @ 20:26pm | #23304

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.