本文整理匯總了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.");
}