Java Tutorial/Data Type/Convert to String
Содержание
- 1 Convert a byte array to base64 string
- 2 Convert Characters to Lower Case
- 3 Convert Characters to Upper Case
- 4 Converting a Primitive Type Value to a String
- 5 Converting byte to String
- 6 Converting Char array to String
- 7 Converting double to String
- 8 Converting float to String
- 9 Converting Integer to Hex String
- 10 Converting int to binary string
- 11 Converting int to String
- 12 Converting long to String
- 13 Converting short to String
- 14 Convert int to Octal String
- 15 Convert String to java int Example
- 16 valueOf(): convert to String
Convert a byte array to base64 string
public class Main {
public static void main(String[] argv) throws Exception {
byte[] buf = new byte[] { 0x12, 0x23 };
String s = new sun.misc.BASE64Encoder().encode(buf);
}
}
Convert Characters to Lower Case
public class Main {
public static void main(String[] args) {
String str = "This Is a Test";
str = str.toLowerCase();
System.out.println(str);
}
}
//this is a test
Convert Characters to Upper Case
public class Main {
public static void main(String[] args) {
String str = "this is a test";
str = str.toUpperCase();
System.out.println(str);
}
}
//THIS IS A TEST
Converting a Primitive Type Value to a String
public class Main {
public static void main(String[] argv) throws Exception {
// Use String.valueOf()
String s = String.valueOf(true);
s = String.valueOf((byte) 0x11);
s = String.valueOf((byte) 0xFF);
s = String.valueOf("a");
s = String.valueOf((short) 123);
s = String.valueOf(123);
s = String.valueOf(123L);
s = String.valueOf(1.23F);
s = String.valueOf(1.23D);
// Use +
s = "" + true;
s = "" + ((byte) 0x12);
s = "" + ((byte) 0xFF);
s = "" + "a";
s = "" + ((short) 123);
s = "" + 111;
s = "" + 111L;
s = "" + 1.11F;
s = "" + 1.11D;
}
}
Converting byte to String
public class MainClass {
public static void main(String[] arg) {
byte b = 12;
System.out.println(String.valueOf(b));
}
}
12
Converting Char array to String
public class MainClass {
public static void main(String[] arg) {
char[] ch = {"a","b","c","d"};
System.out.println(String.valueOf(ch));
}
}
abcd
Converting double to String
public class MainClass {
public static void main(String[] arg) {
double f = 12.13145D;
System.out.println(String.valueOf(f));
}
}
12.13145
Converting float to String
public class MainClass {
public static void main(String[] arg) {
float f = 12.13145F;
System.out.println(String.valueOf(f));
}
}
12.13145
Converting Integer to Hex String
public class MainClass{
public static void main(String[] arg){
System.out.println(Integer.toHexString(10));
System.out.println(Integer.toHexString(20));
System.out.println(Integer.toHexString(30));
System.out.println(Integer.toHexString(40));
}
}
a 14 1e 28
Converting int to binary string
public class MainClass {
public static void main(String[] arg) {
System.out.println(Integer.toBinaryString(100));
}
}
1100100
Converting int to String
public class MainClass {
public static void main(String[] arg) {
int b = 12;
System.out.println(String.valueOf(b));
}
}
12
Converting long to String
public class MainClass {
public static void main(String[] arg) {
long b = 12L;
System.out.println(String.valueOf(b));
}
}
12
Converting short to String
public class MainClass {
public static void main(String[] arg) {
short f = 12;
System.out.println(String.valueOf(f));
}
}
12
Convert int to Octal String
public class MainClass {
public static void main(String[] arg) {
System.out.println(Integer.toOctalString(10));
}
}
12
Convert String to java int Example
public class Main {
public static void main(String[] args) {
String str = new String("10");
int i = Integer.parseInt(str);
System.out.println(i);
}
}
valueOf(): convert to String
public class MainClass {
public static void main(String[] arg) {
byte b = 12;
System.out.println(String.valueOf(b));
}
}
12