변환 중 int를 문자열로 중요한 유형 변환입니다. 문자열에 대해 많은 작업을 수행할 수 있지만 정수의 경우에는 제한이 있습니다. String 클래스에는 번거로움 없이 작업을 수행하는 데 도움이 되는 다양한 내장 메서드 목록이 있습니다.
두 개의 정수를 연결해야 한다고 가정하면 이는 지루한 일이 될 것입니다. 우리가 수체계 안에서 수학을 하게 될 것에 대응하는 수체계를 다루어야 하기 때문에 이 과정을 거쳐야 합니다. 하지만 에게 Java에서 정수를 문자열로 변환하려면 작업을 너무 쉽게 만들어주는 몇 가지 내장 메서드와 클래스가 있습니다.
팁: 우리는 일반적으로 기본 클래스 데이터 멤버 유형을 변환합니다. 래퍼 클래스 Java의 실제 프로그래밍에서는 문자열을 다루기 때문에 문자열을 사용합니다.
Java에서 int를 문자열로 변환하는 방법
변환 중 정수를 문자열로 Integer 클래스를 사용하는 것과 관련됩니다. toString() 또는 문자열.값() 직접 변환을 위해. 문자열.형식() 유연한 서식 옵션을 제공하는 또 다른 방법입니다. 사용 스트링빌더 또는 문자열 버퍼 광범위한 문자열 조작에는 정수 값을 문자열로 추가하는 것이 효율적입니다.
아래에 언급된 정수를 문자열로 변환하는 특정 방법이 있습니다.
- 사용하여 toString() Integer 클래스의 메서드
- 사용하여 가치() String 클래스의 메소드
- 사용 정수(int).toString() Integer 클래스의 메소드
- 빈 문자열과 연결을 사용합니다.
1. Integer 클래스의 toString() 메소드 사용
Integer 클래스에는 정적 메소드가 있습니다. toString() 이는 지정된 int 매개변수를 나타내는 String 객체를 반환합니다. 인수는 변환되어 문자열 인스턴스로 반환됩니다. 숫자가 음수이면 부호가 유지됩니다.
예:
Java// Java Program to Illustrate Integer to String Conversions // Using toString() Method of Integer Class class Geeks { // Main driver method public static void main(String args[]) { // Custom input integers int a = 1234; int b = -1234; // Converting integer to string // using toString() method String str1 = Integer.toString(a); String str2 = Integer.toString(b); // Printing the above strings that // holds integer System.out.println('String str1 = ' + str1); System.out.println('String str2 = ' + str2); } }
산출
String str1 = 1234 String str2 = -1234
2. String 클래스의 valueOf() 메소드 사용
String 클래스에는 정적 메소드가 있습니다. 가치() 아래와 같이 정수를 문자열로 변환하는 데 사용할 수 있습니다.
예:
Java// Java Program to Illustrate Integer to String Conversions // Using valueOf() Method of String class class Geeks { // Main driver method public static void main(String args[]) { // Custom integer input int c = 1234; // Converting above integer to string // using valueOf() Method String str3 = String.valueOf(c); // Printing the integer stored in above string System.out.println('String str3 = ' + str3); } }
산출
String str3 = 1234
3. 사용 정수(int).toString() 정수 클래스의 방법
이는 위에서 제안한 방법 1과 다릅니다. 이 방법에서는 Integer 클래스의 인스턴스를 사용하여 해당 toString() 메서드를 호출합니다.
예:
Java// Java Program to Illustrate // Integer to String Conversions // Using toString() Method of // Integer Class // Importing required classes import java.util.*; // Main class class Geeks { // Main driver method public static void main(String args[]) { // Custom input integer int d = 1234; // Converting integer to string // using toString() method of Integer class String str4 = new Integer(d).toString(); // Printing the integer value stored in above string System.out.println('String str4 = ' + str4); } }
산출
String str4 = 1234
설명: 변수가 기본 유형(int)인 경우 Integer.toString(int) 또는 String.valueOf(int)를 사용하는 것이 더 좋습니다. 그러나 변수가 이미 Integer(기본 유형 int의 래퍼 클래스)의 인스턴스인 경우 위에 표시된 대로 toString() 메서드를 호출하는 것이 더 좋습니다.
메모: 변환이 수행되기 전에 Integer 클래스의 인스턴스가 생성되므로 이 방법은 효율적이지 않습니다. 그리고 더 이상 사용되지 않으며 제거로 표시됩니다.
4. 빈 문자열과 연결 사용
여기서는 빈 문자열을 선언하고 '+' 연산자를 사용하여 결과를 문자열로 저장합니다. 이제 이 문자열을 성공적으로 추가하고 연결할 수 있습니다.
예:
Java// Java Program to Illustrate Integer to String Conversions // Using Concatenation with Empty String class Geeks { // Main driver method public static void main(String args[]) { // Custom integer values int a = 1234; int b = -1234; // Concatenating with empty strings String str1 = '' + a; String str2 = '' + b; // Printing the concatenated strings System.out.println('String str1 = ' + str1); System.out.println('String str2 = ' + str2); } }
산출
String str1 = 1234 String str2 = -1234
int를 문자열로 변환하는 고급 방법
특정 사전 방법이 아래에 언급되어 있습니다:
kmp 알고리즘
- DecimalFormat 클래스 사용
- StringBuffer 클래스 사용
- StringBuilder 클래스 사용
- 특수 기수 및 사용자 정의 기수 사용
1. DecimalFormat 클래스 사용
십진수 형식 숫자를 문자열로 형식화하는 클래스입니다.
예:
Java// Java Program to Illustrate // Integer to String Conversions // Using DecimalFormat Class // Importing required classes import java.text.DecimalFormat; // Main class class Geeks { // Main driver method public static void main(String args[]) { // Input integer value int e = 12345; // Creating an object of DecimalFormat class // inside main() method DecimalFormat df = new DecimalFormat('####'); // Converting above integral value to string String Str5 = df.format(e); // Printing the value stored in above string System.out.println(Str5); } }
산출
12345
팁: 이 방법을 사용하면 가독성을 위해 소수점 이하 자릿수와 쉼표 구분 기호를 지정할 수 있습니다.
2. StringBuffer 클래스 사용
문자열 버퍼 여러 값을 문자열로 연결하는 데 사용되는 클래스입니다.
예시 1:
자바 스위치 케이스Java
// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class // Main class class Geeks { // Main driver method public static void main(String args[]) { // Integer input value int f = 1234; // Creating an object of StringBuffer class StringBuffer sb = new StringBuffer(); sb.append(f); String str6 = sb.toString(); System.out.println('String str6 = ' + str6); } }
산출
String str6 = 1234
예 2:
Java// Java Program to Illustrate // Integer to String Conversions // Using StringBuffer Class class Geeks { // Main driver method public static void main(String args[]) { String str6 = new StringBuffer().append(1234).toString(); System.out.println('String str6 = ' + str6); } }
산출
String str6 = 1234
3. StringBuilder 클래스 사용
스트링빌더 유사하게 작동하지만 StringBuffer처럼 스레드로부터 안전하지 않습니다.
예시 1:
Java// Java Program to Illustrate // Integer to String Conversions // Using StringBuilder Class // Main class class Geeks { // Main driver method public static void main(String args[]) { // Input integer int g = 1234; // Creating an object of StringBuilder class // inside main() method StringBuilder sb = new StringBuilder(); sb.append(g); String str7 = sb.toString(); // Printing the value stored in above string System.out.println('String str7 = ' + str7); } }
산출
String str7 = 1234
예 2:
Java// Java Program to Illustrate Different Ways for // Integer to String Conversions // Using StringBuilder Class // Main class class Geeks { // Main driver method public static void main(String args[]) { String str7 = new StringBuilder().append(1234).toString(); // Printing the value stored in above string System.out.println('String str7 = ' + str7); } }
산출
String str7 = 1234
메모: 위의 모든 예제에서는 기수(radix) 10을 사용합니다. 다음은 2진수 8진수 및 16진수 시스템으로 변환하는 편리한 방법입니다. 임의의 사용자 정의 번호 시스템도 지원됩니다.
int를 다른 기본 문자열로 변환
int를 다른 기준으로 String으로 변환할 수도 있습니다. 아래에는 int를 다른 기준으로 String으로 변환하는 데 도움이 되는 몇 가지 중요한 방법이 언급되어 있습니다.
1. 특수 기수 사용
예 1 : 다음을 사용하여 int(base 2 또는 이진수)를 문자열로 변환 정수 클래스 메소드 toBinary().
Java// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Binary Numbers // Main class class Geeks { // Main driver method public static void main(String args[]) { // Input integer int h = 255; String binaryString = Integer.toBinaryString(h); // Printing the binary number stored in above string System.out.println(binaryString); } }
산출
11111111
설명 : 11111111은 숫자 255를 이진수로 표현한 것입니다.
예 2: 다음을 사용하여 int(base 8 또는 8진수)를 문자열로 변환 정수 클래스 toOctalString() 메소드.
Java// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Octal Numbers // Main class class Geeks { // Main driver method public static void main(String args[]) { // Custom input integer int i = 255; String octalString = Integer.toOctalString(i); // Printing the octal number stored in above string System.out.println(octalString); } }
산출
377
설명: 377은 숫자 255를 8진수로 표현한 것입니다.
예시 3: 다음을 사용하여 int(기본 10 또는 16진수)를 문자열로 변환 정수 클래스 toHexString() 메소드.
Java// Java Program to Illustrate Integer to String Conversions // Using Special Radix In Hexadecimal Numbers // Main class class Geeks { // Main driver method public static void main(String args[]) { // Custom input integer int j = 255; String hexString = Integer.toHexString(j); // Printing the hexadecimal number // stored in above string System.out.println(hexString); } }
산출
ff
설명: 그만큼 ff 숫자 255를 16진수로 표현한 것입니다.
2. 사용자 정의 기본/기수
접근하다: Integer 클래스의 toString() 메서드를 사용하여 문자열로 변환하고 추가적으로 기수라는 인수로 값을 전달합니다. int를 문자열로 변환할 때 다른 사용자 정의 기본/기수를 사용할 수 있습니다. 아래 예에서는 설명 목적으로 기본 7 숫자 시스템을 고려하고 있습니다.
예: 다음을 사용하여 int(기본 7 또는 사용자 정의 기수 수)를 문자열로 변환 정수 클래스 메소드 toString(int I int 기수).
Java// Java Program to Illustrate Integer to String Conversions // Using Custom Radix // Main class class Geeks { // Main driver method public static void main(String args[]) { // Input integer value int k = 255; // Setting base as 7 converting integer to string // using toString() method and // storing it into a string String customString = Integer.toString(k 7); // Printing value stored in above string System.out.println(customString); } }
산출
513
메모: 513은 7진수 체계로 쓰여졌을 때 숫자 255를 표현한 것입니다.