我在Java中得到了一个简单的问题:如何将long . tostring()获得的字符串转换为长字符串?


当前回答

这很简单,使用 长。返回对象的值(字符串);

例如:

String s;
long l;

Scanner sc=new Scanner(System.in);
s=sc.next();
l=Long.valueOf(s);
System.out.print(l);

你做完了! !

其他回答

长。valueOf(String s) -显然,如果在代码中可能出现非数字,则必须采取适当的保护措施。

public class StringToLong {

   public static void main (String[] args) {

      // String s = "fred";    // do this if you want an exception

      String s = "100";

      try {
         long l = Long.parseLong(s);
         System.out.println("long l = " + l);
      } catch (NumberFormatException nfe) {
         System.out.println("NumberFormatException: " + nfe.getMessage());
      }

   }
}

对于那些切换到Kotlin的人,只需使用 string.toLong () 这将在引擎盖下调用Long.parseLong(string)

使用Long.parseLong ()

 Long.parseLong("0", 10)        // returns 0L
 Long.parseLong("473", 10)      // returns 473L
 Long.parseLong("-0", 10)       // returns 0L
 Long.parseLong("-FF", 16)      // returns -255L
 Long.parseLong("1100110", 2)   // returns 102L
 Long.parseLong("99", 8)        // throws a NumberFormatException
 Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
 Long.parseLong("Hazelnut", 36) // returns 1356099454469L
 Long.parseLong("999")          // returns 999L

这很简单,使用 长。返回对象的值(字符串);

例如:

String s;
long l;

Scanner sc=new Scanner(System.in);
s=sc.next();
l=Long.valueOf(s);
System.out.print(l);

你做完了! !