将整形值转换成字符串

将整形值转换成字符串,有3钟方法

  1. String.ValueOf()
    String类的valueOf()方法实现了将多种类型的值转换为字符串

  2. Integer.toString()
    Integer类的toString()方法中包含一个int类型的参数,当使用该方法时,可以直接调用该方法并传入一个int类型的参数,即可将int值转换为字符串

  3. Integer.valueOf().toString()
    使用Integer类的valueOf().toString()方法时,首先将int值转换成Integer对象,然后再调用toString()方法即可转换为字符串
    在这里插入图片描述
    创建StringUtil的JavaBean类,实现将int值转换为字符串的3种方法
    public class StringUtil6 {
    private int intValue=0; //转换前的int值
    private String strValue1; //转换后的字符串1值
    private String strValue2; //转换后的字符串2值
    private String strValue3; //转换后的字符串3值
    public StringUtil6(){} //默认构造方法
    public int getIntValue() {
    return intValue;
    }
    public void setIntValue(int intValue) {
    this.intValue = intValue;
    }
    public String getStrValue1() {
    return String.valueOf(intValue);//使用String类的valueOf方法转换
    }
    public void setStrValue1(String strValue1) {
    this.strValue1 = strValue1;
    }
    public String getStrValue2() {
    return Integer.toString(intValue);//使用Integer类的toString方法转换
    }
    public void setStrValue2(String strValue2) {
    this.strValue2 = strValue2;
    }
    public String getStrValue3() {
    return Integer.valueOf(intValue).toString(); //使用Integer类的valueOf方法,然后再使用toString方法
    }
    public void setStrValue3(String strValue3) {
    this.strValue3 = strValue3;
    }

    }
    创建index.jsp页面,调用StringUtil类的3种方法返回字符串值

   <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>将整型值转换为字符串</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

	<style type="text/css">
		table{
			border: 1px solid;
			border-color: green;
			font-family:华文细黑;
			font-size: 15px;
			color:black;
			background-color: white;
		}
	</style>
  </head>
  
  <body>
  
  	 <%
  		int userAge = 35;
  	 %>
     <!-- 使用useBean动作导入StringUtil类 -->
     <jsp:useBean id="strBean" class="com.cn.zj.bean.StringUtil6"></jsp:useBean>
     <!-- 对StringUtil类对象中的intValue属性赋值 -->
     <jsp:setProperty property="intValue" name="strBean" value="<%=userAge %>"/>
  		<table>
  			<tr bgcolor="skyblue">
  				<td align="center">使用String.valueOf()方法转换int值:š</td>
  			</tr>
  			<tr>
  				<td align="center">
  					<!-- 获得StringUtil类对象中的strValue1属性值 -->
  					年龄1:<jsp:getProperty property="strValue1" name="strBean"/>岁
  				</td>
  			</tr>
  			<tr bgcolor="skyblue">
  				<td align="center">使用Integer.toString()方法转换int值:š</td>
  			</tr>
  			<tr>
  				<td align="center">
  					<!-- 获得StringUtil类对象中的strValue2属性值 -->
  					年龄2:<jsp:getProperty property="strValue2" name="strBean"/>岁
  				</td>
  			</tr>
  			<tr bgcolor="skyblue">
  				<td align="center">使用Integer.valueOf().toString()方法转换int值:š</td>
  			</tr>
  			<tr>
  				<td align="center">
  					<!-- 获得StringUtil类对象中的strValue3属性值 -->
  					年龄3:<jsp:getProperty property="strValue3" name="strBean"/>岁
  				</td>
  			</tr>
  		</table>		
  </body>
</html>
  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值