本文整理汇总了Java中java.lang.String.trim方法的典型用法代码示例。如果您正苦于以下问题:Java String.trim方法的具体用法?Java String.trim怎么用?Java String.trim使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.String
的用法示例。
在下文中一共展示了String.trim方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: castToInt
import java.lang.String; //导入方法依赖的package包/类
public static int castToInt(String s)
{
if (s == null)
return 0;
if (s.equals("INF") || s.equals("-INF") || s.equals("NaN"))
throw new ArithmeticException("'" + s + "' is too large for int.");
s = s.trim();
java.lang.StringBuffer buf = new java.lang.StringBuffer();
switch (prepareNumber(buf, s))
{
case 1:
return Integer.parseInt(buf.toString());
case 2:
return new BigDecimal(buf.toString()).intValue();
case 3:
return (int)Double.parseDouble(buf.toString());
default:
throw new NumberFormatException("'" + s + "' cannot be converted to int.");
}
}
示例2: castToLong
import java.lang.String; //导入方法依赖的package包/类
public static long castToLong(String s)
{
if (s == null)
return 0;
if (s.equals("INF") || s.equals("-INF") || s.equals("NaN"))
throw new ArithmeticException("'" + s + "' is too large for long.");
s = s.trim();
java.lang.StringBuffer buf = new java.lang.StringBuffer();
switch (prepareNumber(buf, s))
{
case 1:
return Long.parseLong(buf.toString());
case 2:
return new BigDecimal(buf.toString()).longValue();
case 3:
return (long)Double.parseDouble(buf.toString());
default:
throw new NumberFormatException("'" + s + "' cannot be converted to long.");
}
}
示例3: castToBigInteger
import java.lang.String; //导入方法依赖的package包/类
public static BigInteger castToBigInteger(String s)
{
if (s == null)
return null;
if (s.equals("INF") || s.equals("-INF") || s.equals("NaN"))
throw new ArithmeticException("'" + s + "' is too large for integer.");
s = s.trim();
java.lang.StringBuffer buf = new java.lang.StringBuffer();
switch (prepareNumber(buf, s))
{
case 1:
return new BigInteger(buf.toString());
case 2:
case 3:
return new BigDecimal(buf.toString()).toBigInteger();
default:
throw new NumberFormatException("'" + s + "' cannot be converted to integer.");
}
}
示例4: castToBigDecimal
import java.lang.String; //导入方法依赖的package包/类
public static BigDecimal castToBigDecimal(String s)
{
if (s == null)
return null;
if (s.equals("INF") || s.equals("-INF") || s.equals("NaN"))
throw new ArithmeticException("'" + s + "' is too large for decimal.");
s = s.trim();
java.lang.StringBuffer buf = new java.lang.StringBuffer();
switch (prepareNumber(buf, s))
{
case 1:
case 2:
case 3:
return new BigDecimal(buf.toString());
default:
throw new NumberFormatException("'" + s + "' cannot be converted to decimal.");
}
}
示例5: castToDouble
import java.lang.String; //导入方法依赖的package包/类
public static double castToDouble(String s)
{
if (s == null)
return 0;
if (s.equals("INF"))
return Double.POSITIVE_INFINITY;
else if (s.equals("-INF"))
return Double.NEGATIVE_INFINITY;
else if (s.equals("NaN"))
return Double.NaN;
s = s.trim();
java.lang.StringBuffer buf = new java.lang.StringBuffer();
switch (prepareNumber(buf, s))
{
case 1:
case 2:
case 3:
return Double.parseDouble(buf.toString());
default:
throw new NumberFormatException("'" + s + "' cannot be converted to double.");
}
}
示例6: NumberParts
import java.lang.String; //导入方法依赖的package包/类
public NumberParts(String input)
{
input = input.trim();
if (input.startsWith("-"))
{
sign = "-";
input = input.substring(1);
}
else if (input.startsWith("+"))
{
sign = "";
input = input.substring(1);
}
else
sign = "";
int indexOfE = input.indexOf('e');
if (indexOfE < 0)
indexOfE = input.indexOf('E');
if (indexOfE < 0)
{
mantissa = input;
exponent = "";
}
else
{
mantissa = input.substring(0, indexOfE);
exponent = input.substring(indexOfE + 1);
}
if (mantissa.startsWith("."))
mantissa = "0" + mantissa;
if (mantissa.endsWith("."))
mantissa = mantissa + "0";
if (mantissa.length() == 0)
mantissa = "0";
if (exponent.endsWith("+") || exponent.endsWith("-"))
exponent = exponent + "0";
}
示例7: castToBool
import java.lang.String; //导入方法依赖的package包/类
public static boolean castToBool(String v)
{
String s = v.trim();
if (s.equals("true") || s.equals("1")) return true;
if (s.equals("false") || s.equals("0")) return false;
throw new IllegalArgumentException("'" + v + "' cannot be parsed as boolean.");
}