您的位置:首页 > 其它

Lifyray笑傲江湖之API总结TextUtil

2015-09-16 11:13 344 查看
package liferay;

/**
*
*/

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Ivan
*
*/
public class TextUtil {

private static Pattern N = Pattern.compile("\n");
private static Pattern R = Pattern.compile("\r");
private static Pattern RN = Pattern.compile("\r\n");
private static Pattern NR = Pattern.compile("\n\r");
private final static String BR = "<br/>";

private static NumberFormat decimalFormat = DecimalFormat
.getNumberInstance();

public static String doubleToText(double number) {
String txt = doubleToText(number, 1);
return txt;
}

public static String doubleToText(double number, int digit) {
String txt = "";
if (number > 0) {
decimalFormat.setMaximumFractionDigits(digit);
txt = decimalFormat.format(number);
}
return txt;
}

public static String doubleToText2(double number, int digit) {
String txt = "";
if (number > 0) {
DecimalFormat decimalFormat2 = new DecimalFormat();
decimalFormat2.setGroupingSize(0);
decimalFormat2.setMaximumFractionDigits(digit);
txt = decimalFormat2.format(number);
}
return txt;
}

public static String doubleToText(double number, String format) {
if (format.equals("")) {
format = "#,##0.00";
}

String txt = "";
if (number > 0) {
DecimalFormat decimalFormat = new DecimalFormat(format);
txt = decimalFormat.format(number);
}
return txt;
}

public static String textToHtml(String txt) {
Matcher m = RN.matcher(txt);
txt = m.replaceAll(BR);
m = NR.matcher(txt);
txt = m.replaceAll(BR);
m = N.matcher(txt);
txt = m.replaceAll(BR);
m = R.matcher(txt);
txt = m.replaceAll(BR);
return txt;
}

public static void main(String[] args) {
//        long longDuration = new Date().getTime() - 46450000;
//        longDuration = new Date().getTime() - longDuration;
//        double doubleDuration = longDuration;
//        doubleDuration = doubleDuration / 86400000;
//        System.out.println(doubleDuration);
//        System.out.println(doubleToText(doubleDuration));

String sql = getRandomString(5);
System.out.println(sql);

}

public static String doSubAssignLength(String str, int length) {
String commtents = null;
if (str.length() > length) {
commtents = str.substring(0, length - 1) + "...";
} else {
commtents = str;
}
return commtents;
}

public static String doSubString(String str) {
String commtents = null;
if (str.length() > 26) {
commtents = str.substring(0, 25) + "...";
} else {
commtents = str;
}
return commtents;
}

public static String getURL(String layoutId, String url) {
StringBuffer buffer = new StringBuffer();
buffer.append(url);
if (layoutId != null && !"0".equals(layoutId)) {
int start = url.lastIndexOf("/");
int end = url.indexOf("?");
buffer.replace(start, end, layoutId);
}
return buffer.toString();
}

public static String toUpperCase(String tableName) {
String prefix = tableName.substring(0, 1).toUpperCase();
String results = prefix + tableName.substring(1);
return results;
}

public static String getRandomString(int length) {
char[] charArray = new char[length];
for (int i = 0; i < length; i++) {
Random r = new Random();
int n = r.nextInt(123);
while (n < 48 || (n > 57 && n < 65) || (n > 90 && n < 97)
|| n > 122) {// (!((n>=48 && n<=57) || (n>=65 && n<=90)
// && (n>=97 && n<=122))){
n = r.nextInt(123);
}
charArray[i] = (char) n;
}
return String.valueOf(charArray);
}

public static String convertFileSize(long filesize) {
String strUnit = "Bytes";
String strAfterComma = "";
int intDivisor = 1;
if (filesize >= 1024 * 1024) {
strUnit = " MB";
intDivisor = 1024 * 1024;
} else if (filesize >= 1024) {
strUnit = " KB";
intDivisor = 1024;
}
if (intDivisor == 1)
return filesize + " " + strUnit;

strAfterComma = "" + 100 * (filesize % intDivisor) / intDivisor;
if (strAfterComma == "")
strAfterComma = ".0";

return filesize / intDivisor + "." + strAfterComma + " " + strUnit;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: