Sometimes we can view a certain number for users. We often use a sub -display, that is, every few numbers are separated by comma.
We can apply the method in the StringBuffer class to implement: (we are used to starting from behind)
1.insert();
This method can be dynamically inserted into the specified string in a specified position in a string of a StringBuffer object type.
2.reverse();
This method can reverse the string.
1.JavaBean:
StringUtil.java
package exa134;
public class StringUtil {
private long longValue; // Numbers to be divided
private int digit; // division digits
private String formatStr; // The string after division
public long getLongValue() {
return longValue;
}
public void setLongValue(long longValue) {
this.longValue = longValue;
}
public int getDigit() {
return digit;
}
public void setDigit(int digit) {
this.digit = digit;
}
public String getFormatStr() {
// Convert the value of the LONG type to a dynamic modified StringBuffer object
StringBuffer sb = new StringBuffer(String.valueOf(longValue));
sb =sb.reverse(); // Reverse the string
int l = sb.length();
if(digit==0){
// If the number of points is 0, the length of the set string is the segmentation bit
digit=l;
}
int count = 0;
/** Calculate the number of semi -subtraction of the insertion* /
if(l%digit==0)
count=l/digit-1;
else
count=l/digit;
for(int i= 0;i<count;i++){
sb =sb.insert((i+1)*digit+i ,",");// Insert seminars
}
return sb.reverse().toString();
}
public void setFormatStr(String formatStr) {
this.formatStr = formatStr;
}
}
2.jsp file:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="format.jsp" method="post">
<table>
<tr>
<td align="right">Please enter the number:</td>
<td><input type="text" name="longValue" /></td>
</tr>
<tr>
<td align="right">Please enter the number of points:</td>
<td><input type="text" name="digit" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Sub -Display" /></td>
</tr>
</table>
</form>
</body>
</html>
format.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String longValueStr = request.getParameter("longValue");// Get the long number string in the form
String Digit= request.getParameter("digit");// Get the segmentation digital string
long longvalue= Long.parseLong(longValueStr);// Convert the long -numbered string to the Long type
int D= Integer.parseInt(digit);// Convert the sub -position digital strings to int type
%>
<!-Use the usebean action label to import the JavaBean object ->
<jsp:useBean id="strBean" class="exa134.StringUtil"></jsp:useBean>
<!-LONGVALUE attributes of the StringUtil class ->
<jsp:setProperty property="longValue" name="strBean" value="<%=longValue %>"/>
<!-Digit attributes to the StringUtil class of the StringUtil class-
The number before>
<jsp:setProperty property="digit" name="strBean" value="<%=d %>"/>
<table>
<tr >
<td >:
Number after</td>
<td align="left">
<jsp:getProperty property="longValue" name="strBean"/>
</td>
</tr>
<tr >
<td >:</td>
<td align="left">
<jsp:getProperty property="formatStr" name="strBean"/>
</td>
</tr>
</table>
</body>
</html>
result: