Today we will see different ways to convert long to string in java. Java long to string conversion can be done in many ways, we will go through them one by one with example code snippets.
今天,我们将看到在Java中将long转换为string的不同方法。 Java长字符串转换可以通过多种方式完成,我们将通过示例代码片段逐一介绍它们。
Java Long to String (Java Long to String)
Let’s look at different code snippets for java long to string conversion. Note that long
is a primitive data type whereas Long
is an Object. However java supports autoboxing, so they both can be used interchangeably in most of the cases.
让我们看一下从Java长到字符串转换的不同代码段。 请注意, long
是原始数据类型,而Long
是对象。 但是java支持autoboxing ,因此它们在大多数情况下都可以互换使用。
使用+运算符 (Using + operator)
This is the easiest way to convert long to string in java.
long l = 123L; String str = l+""; // str is '123'
这是在Java中将long转换为字符串的最简单方法。
Long.toString() (Long.toString())
We can use Long class
toString
method to get the string representation of long in decimal points. Below code snippet shows you how to use it to convert long to string in java.long l = 12345L; String str = Long.toString(l); System.out.println(str); //prints '12345'
We can write long in other formats too such as Octal and Hexadecimal, let’s see what happens when we convert long to string in those scenarios.
So the string is always being returned in decimal format. Let’s look into other ways to convert long to string too.
我们可以使用Long类的
toString
方法获取小数点中long的字符串表示形式。 下面的代码段显示了如何在Java中使用它将long转换为字符串 。long l = 12345L; String str = Long.toString(l); System.out.println(str); //prints '12345'
我们也可以用八进制和十六进制等其他格式写长整型,让我们看看在这种情况下将长整型转换为字符串会发生什么。
因此,字符串始终以十进制格式返回。 让我们看看其他将long转换为string的方法。
String.valueOf() (String.valueOf())
long l = 12345L; String str = String.valueOf(l); // str is '12345'
新长(long l) (new Long(long l))
Long constructor with long argument has been deprecated in Java 9, but you should know it.
long l = 12345L; //deprecated from Java 9, use valueOf for better performance String str = new Long(l).toString(); // str is '12345'
Java 9已弃用带有long参数的long构造函数,但是您应该知道这一点。
String.format() (String.format())
long l = 369L; String s = String.format("%d", l);
小数格式 (DecimalFormat)
long l = 12345L; String str = DecimalFormat.getNumberInstance().format(l); System.out.println(str); //str is '12,345' //if you don't want formatting str = new DecimalFormat("#").format(l); System.out.println(str); //str is '12345'
StringBuilder,StringBuffer (StringBuilder, StringBuffer)
We can use StringBuilder and StringBuffer append function to convert long to string.
long l = 12345L; String str = new StringBuilder().append(l).toString();
我们可以使用StringBuilder和StringBuffer append函数将long转换为字符串。
Java Long to String示例 (Java Long to String Example)
Here is a simple program where we will convert long to string and print it using all the different methods we saw above.
这是一个简单的程序,我们将把long转换为字符串并使用上面看到的所有不同方法来打印它。
package com.journaldev.string;
import java.text.DecimalFormat;
public class JavaLongToString {
@SuppressWarnings("deprecation")
public static void main(String[] args) {
long l = 12345L;
String str = Long.toString(l);
System.out.println(str);
str = String.valueOf(l);
System.out.println(str);
// deprecated from Java 9, use valueOf for better performance
str = new Long(l).toString();
System.out.println(str);
str = String.format("%d", l);
System.out.println(str);
str = l + "";
System.out.println(str);
str = DecimalFormat.getNumberInstance().format(l);
System.out.println(str);
str = new DecimalFormat("#").format(l);
System.out.println(str);
str = new StringBuilder().append(l).toString();
System.out.println(str);
}
}
Change the long value in above program to Octal or Hexadecimal format, you will notice that string representation is always on decimal format.
将上述程序中的long值更改为八进制或十六进制格式,您会注意到字符串表示始终为十进制格式。
使用基数将Long转换为字符串 (Convert Long to String with Radix)
What if we want to convert long to string with different radix, not the default decimal point. We can use Long class methods for these cases. Below code snippet shows how to get the string representation of a long variable in different bases such as Octal, Hex or even custom one.
如果我们想将long转换为具有不同基数的字符串,而不是默认小数点,该怎么办? 对于这些情况,我们可以使用Long类方法。 下面的代码片段显示了如何以不同的基数(例如,八进制,十六进制甚至是自定义变量)获取长变量的字符串表示形式。
long number = 45;
System.out.println(Long.toBinaryString(number)); //101101
System.out.println(Long.toOctalString(number)); //55
System.out.println(Long.toHexString(number)); //2d
System.out.println(Long.toString(number, 5)); //140
That’s all for converting long to string in java program.
这就是在Java程序中将long转换为字符串的全部方法。
Reference: Long API Doc
参考: Long API文档