java字符串长度_Java字符串长

java字符串长度

Java String to long conversion can be done by many ways. Today we will look into some common ways to convert java string to long primitive data type or Long object. Note that since java supports autoboxing, long primitive type and Long object can be used interchangeably without any issues.

Java String到long的转换可以通过多种方法完成。 今天,我们将研究将Java字符串转换为长原始数据类型或Long对象的一些常用方法。 请注意,由于Java支持自动装箱,因此long原始类型和Long对象可以互换使用而没有任何问题。

Java字符串长 (Java String to Long)

java string to long

Let’s look at all the different ways to convert string to long in java.


让我们看看在Java中将字符串转换为long的所有不同方法。

  1. Long.parseLong() (Long.parseLong())

    We can parse String to long using parseLong() method. Note that string should not contain “L” at the end as type indicator or else it will throw NumberFormatException. However string can start with “-” to denote negative number or “+” to denote positive number. This method returns long primitive type. Below code snippet shows how to convert string to long using Long.parseLong() method.

    String str = "-12345";
    long l = Long.parseLong(str); // returns long primitive

    我们可以使用parseLong()方法将String解析为long。 请注意,字符串结尾不应包含“ L”作为类型指示符,否则它将引发NumberFormatException 。 但是字符串可以以“-”开头表示负数或以“ +”开头表示正数。 此方法返回长原始类型。 下面的代码片段显示了如何使用Long.parseLong()方法将字符串转换为long。

  2. Long.valueOf() (Long.valueOf())

    This method works almost similar as parseLong() method, except that it returns Long object. Let’s see how to use this method to convert String to Long object.

    String str = "12345";
    Long l = Long.valueOf(str); // returns Long object

    此方法与parseLong()方法几乎一样,不同之处在于它返回Long对象。 让我们看看如何使用此方法将String转换为Long对象。

  3. 新的Long(String s) (new Long(String s))

    We can convert String to Long object through it’s constructor too. Also if we want long primitive type, then we can use longValue() method on it. Note that this constructor has been deprecated in Java 9, preferred approach is to use parseLong() or valueOf() methods.

    String str = "987";
    long l = new Long(str).longValue(); //constructor deprecated in java 9

    我们也可以通过其构造函数将String转换为Long对象。 同样,如果我们想要长基本类型,则可以在其上使用longValue()方法。 请注意,此构造函数已在Java 9中弃用,首选方法是使用parseLong()valueOf()方法。

  4. DecimalFormat parse() (DecimalFormat parse())

    This is useful to parse formatted string to long. For example, if String is “1,11,111” then we can use DecimalFormat to parse this string to long as:

    String str = "1,11,111";
    try {
    	long l = DecimalFormat.getNumberInstance().parse(str).longValue();
    	System.out.println(l);
    } catch (ParseException e) {
    	e.printStackTrace();
    }

    Note that parse() method returns instance of Number, so we are calling longValue() to get the long primitive type from it.

    这对于将格式化的字符串解析为long很有用。 例如,如果String为“ 1,11,111”,那么我们可以使用DecimalFormat将此字符串解析为以下形式:

    请注意,parse()方法返回Number实例,因此我们正在调用longValue()以从中获取长longValue()类型。

用基数将字符串转换为Long (Convert String to Long with Radix)

Sometimes numbers are not written in decimal format, they can be in binary, octal or hexadecimal format. There are ways to convert non-decimal radix strings to long by passing radix value. Below code snippet shows how to convert string to long value for binary, octal and hexadecimal string representations.

有时数字不是以十进制格式写的,它们可以是二进制,八进制或十六进制格式。 有一些方法可以通过传递基数值来将非十进制基数字符串转换为long。 下面的代码片段显示了如何将字符串转换为二进制,八进制和十六进制字符串表示形式的long值。

str = "111"; // Binary format (2^2+2+1 = 7)
l = Long.parseLong(str, 2);
System.out.println(l); // prints 7

str = "0111"; // Octal format (8^2+8+1 = 73)
l = Long.parseLong(str, 8);
System.out.println(l); // prints 73

str = "FFF"; // Hexadecimal format (15*16^2+15*16+15 = 4095)
l = Long.parseLong(str, 16);
System.out.println(l); // prints 4095

That’s all for converting string to long in java program.

这就是在Java程序中将字符串转换为long的全部。

Reference: Long API Doc

参考: Long API文档

翻译自: https://www.journaldev.com/18343/java-string-to-long

java字符串长度

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值