开发者

how to format string in jsp?

开发者 https://www.devze.com 2023-02-26 09:21 出处:网络
In the database column1 have date like 2011-03-03 but I want to show only 03-03 and in column2 have string like BHEL.NS but I want to show only BHEL.

In the database column1 have date like 2011-03-03 but I want to show only 03-03 and in column2 have string like BHEL.NS but I want to show only BHEL.

<TD><center><%=rs.getString(1)%></center></TD>  
<TD开发者_开发知识库><center><%=rs.getString(2)%></center></TD>

How to do this?

thanks in advance..


<TD><center><%=rs.getString(1)!=null?rs.getString(1).subString(rs.getString(1).indexOf("-")+1):"-"%></center></TD>  
<TD><center><%=rs.getString(2)!=null?rs.getString(2).subString(0,rs.getString(2).indexOf(".")):"-"%></center></TD> 


I would recommend you to avoid javacode in view. You can have List of your POJOs fetched and filled up from DB , and then you can render it on view using JSTL

And with your current way you can make it working by following way

<TD><center><%=rs.getString(1).subString(rs.getString(1).indexOf("-")+1)%></center></TD>  
<TD><center><%=rs.getString(2).subString(0,rs.getString(2).indexOf("."))%></center></TD>  

Also See

  • JSP Coding convention


For date you can use SimpleDateFormat and for second column use substring function of String.

Or use substring for both as

rs.getString(1).substring(rs.getString(1).indexOf("-")+1)  
rs.getString(2).substring(0,rs.getString(2).indexOf("."))
0

精彩评论

暂无评论...
验证码 换一张
取 消