目录

static short parseShort(String s, int radix)

描述 (Description)

java.lang.Short.parseShort(String s, int radix)方法将字符串参数解析为第二个参数指定的radix的signed short。

声明 (Declaration)

以下是java.lang.Short.parseShort()方法的声明

public static short parseShort(String s, int radix) throws NumberFormatException

参数 (Parameters)

  • s - 这是一个包含要解析的短表示的String。

  • radix - 这是解析s时使用的基数

返回值 (Return Value)

此方法返回指定基数中字符串参数表示的short。

异常 (Exception)

NumberFormatException - 如果字符串不包含可解析的short。

例子 (Example)

以下示例显示了java.lang.Short.parseShort()方法的用法。

package com.iowiki;
import java.lang.*;
public class ShortDemo {
   public static void main(String[] args) {
      String str = "1000";
      // returns signed decimal short value of string
      short shortValue = Short.parseShort(str); 
      // prints signed decimalshort value
      System.out.println("Signed decimal short value for given String is =
         " + shortValue);  
      // returns the string argument as a signed short in the radix
      shortValue = Short.parseShort(str,2);
      System.out.println("Signed decimal short value for specified String
         with radix 2 is = " + shortValue);
      // returns the string argument as a signed short in the radix
      shortValue = Short.parseShort("11",8);
      System.out.println("Signed decimal short value for specified String
         with radix 8 is = " + shortValue);
   }
}   

让我们编译并运行上面的程序,这将产生以下结果 -

Signed decimal short value for given String is = 1000
Signed decimal short value for specified String with radix 2 is = 8
Signed decimal short value for specified String with radix 8 is = 9
↑回到顶部↑
WIKI教程 @2018