Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to split string in JSP?

 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All:
I have a simple problem but don't know how should do it? Hope someone help me!
Q1. I have a stringA="A/SG/Fall"
I want to split it to three string, which are A, SG, Fall,
How do split it out?
Q2. I have another stringB="this is my problem!"
I want to get the last three characters for the StringB, I know in ASP can be use Len Function, but don't know in JSP how to do it?
Thank you very much for all the help.
Eric Wang
 
Satya Narayana
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1. I have a stringA="A/SG/Fall"
I want to split it to three string, which are A, SG, Fall,
How do split it out?
A. Use String Tokenizer
StringTokenizer st=new StringTokenizer(stringA,"/");
While(st.hasMoreTokens())
{
String token=st.nextToken() //Here you will get A, sg, fall as separated tokens
}
Q2. I have another stringB="this is my problem!"
I want to get the last three characters for the StringB, I know in ASP can be use Len Function, but don't know in JSP how to do it?
A2. stringB.substring(stringB.length()-3,stringB.length());
Nothing impossible in Java. Just explore it.
:-)
-SaTyA-
 
Eric Wang
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya Narayana:
Thank you very much for the reply. When I testing the program, it come out this problem in my jsp code, but I never have this problem before.
My code is like this:
<%@ page
import="
java.util.*;
java.io.*;
"%>
Error message:
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile class for JSPE:\tomcat\work\localhost_8080\_0002fSplit_0002ejspSplit_jsp_5.java:15: Class or interface declaration expected.
java.io.*;
^
1 error
Please help!
I really like you said this Nothing impossible in Java.
Regards
Hongwei
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,
Please change ; to , (comma) as following line.
<%@ page import="java.util.*,java.io.*;"%>
regds
maha ana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic