Language/Java

[Java] String to Integer, Integer to String

_Hiiro 2022. 10. 9. 21:52

String to Integer

import java.util.Arrays;

// string to int
String str = "100";
int num = Integer.parseInt(str);


// string array to int Array
String[] strArr = {"100", "200", "300"};
int[] intArr = Arrays.stream(strArr)
                    .mapToInt(Integer::parseInt)	//mapToInt(x->Integer.parseInt(x))
                    .toArray();
System.out.println(Arrays.toString(intArr));	//[100,200,300]

 

Integer to String

import java.util.Arrays;

// int to String
int num = 100;
String str1 = Integer.toString(num);
String str2 = String.valueOf(num);


// int array to String Array
int[] intArr = {100, 200, 300};
String[] strArr = Arrays.stream(intArr)
                    .mapToObj(String::valueOf)	//mapToInt(x->String.valueOf(x))
                    .toArray(String[]::new);
System.out.println(Arrays.toString(strArr));	//[100,200,300]

 

 

 


참고 레퍼런스