java2s_使用Java的严格解析解析s

public class Main {

public static void main(String[] argv) {

String s = "java2s.com";

System.out.println(parseStrict(s));

}

/** Use Java's "strict parsing" methods Integer.parseInt and Double.parseDouble to parse s "strictly". i.e. if it's neither a double or an integer, fail.

* @param s the string to convert

* @return the numeric value

*/

public static Number parseStrict(String s) {

s = s.trim();

if (s.length() == 0)

return 0;

if (s.charAt(0) == '+')

s = s.substring(1);

if (s.matches("(\\+|-)?Infinity")) {

if (s.startsWith("-")) {

return Double.NEGATIVE_INFINITY;

} else {

return Double.POSITIVE_INFINITY;

}

} else if (s.indexOf('.') != -1 || s.equals("-0")) {

return Double.parseDouble(s);

}

// parse hex

else if (s.toLowerCase().indexOf("0x") > -1) {

int coef = s.charAt(0) == '-' ? -1 : 1;

if (s.length() > 17)

throw new RuntimeException(

"Can't handle a number this big: " + s);

// if coef == -1: (coef * -.5 + 2.5) == 3

// e.g., -0xf00 (start substring at 3)

// if coef == 1: (coef * -.5 + 2.5) == 2

// e.g., 0xf00 (start substring at 2)

if (s.length() > 9)

return coef

* Long.parseLong(

s.substring((int) (coef * -.5 + 2.5)), 16);

return coef

* Integer.parseInt(

s.substring((int) (coef * -.5 + 2.5)), 16);

}

int e = s.toLowerCase().indexOf('e');

// parse exp

if (e > 0) {

double num = Double.parseDouble(s.substring(0, e));

int exp = Integer.parseInt(s.substring(e + 1));

return num * Math.pow(10, exp);

}

// parse with smallest possible precision

if (s.length() > 17)

return Double.parseDouble(s);

else if (s.length() > 9)

return Long.parseLong(s);

return Integer.parseInt(s);

}

/** Turns a string into an int and returns a default value if unsuccessful.

* @param s the string to convert

* @param d the default value

* @return the int value

*/

public static int parseInt(String s, int def) {

return parseInt(s, def, null, true);

}

/** Turns a string into an int and returns a default value if unsuccessful.

* @param s the string to convert

* @param d the default value

* @param lastIdx sets lastIdx[0] to the index of the last digit

* @param allowNegative if negative numbers are valid

* @return the int value

*/

public static int parseInt(String s, int def, final int[] lastIdx,

final boolean allowNegative) {

final boolean useLastIdx = lastIdx != null && lastIdx.length > 0;

if (useLastIdx)

lastIdx[0] = -1;

if (s == null)

return def;

s = s.trim();

if (s.length() == 0)

return def;

int firstDigit = -1;

for (int i = 0; i < s.length(); i++) {

if (Character.isDigit(s.charAt(i))) {

firstDigit = i;

break;

}

}

if (firstDigit < 0)

return def;

int lastDigit = firstDigit + 1;

while (lastDigit < s.length()

&& Character.isDigit(s.charAt(lastDigit)))

lastDigit++;

if (allowNegative && firstDigit > 0

&& s.charAt(firstDigit - 1) == '-')

firstDigit--;

if (useLastIdx)

lastIdx[0] = lastDigit;

return Integer.parseInt(s.substring(firstDigit, lastDigit));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值