CodeGym /จาวาบล็อก /สุ่ม /วิธีแปลง int เป็น String ใน Java
John Squirrels
ระดับ
San Francisco

วิธีแปลง int เป็น String ใน Java

เผยแพร่ในกลุ่ม
ในบทความนี้เราจะพูดถึงการแปลง int (ประเภทดั้งเดิม) และประเภทวัตถุ (wrapper) จำนวนเต็มเป็นสตริง มีหลายวิธีที่จะทำใน Java

วิธีแปลง int เป็น string ใน Java

  1. แปลงโดยเพิ่มสตริงว่าง

    วิธีที่ง่ายที่สุดในการแปลง int เป็น String นั้นง่ายมาก เพียงเพิ่มสตริงว่าง "" ลงใน int หรือ Integer แล้วคุณจะได้ int เป็นสตริง มันเกิดขึ้นเพราะการเพิ่ม int และ String ทำให้คุณได้ String ใหม่ นั่นหมายความว่า ถ้าคุณมีint x = 5ก็แค่กำหนดx + ""แล้วคุณจะได้ String ใหม่

    นี่คือตัวอย่าง:

    
    //  converting int to string, Java 
    public class Demo {
       public static void main(String[] args) {
    
           int x = 5;
           //  java int to string
           String xText = x + "";
           //  the result output
           System.out.println("convert int to String, Java: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("adding int = 5 and String = \"5\". The result is a new String = " + xText + x);
           //  integer to string, Java code
           Integer y = 7;
           String yText = y + "";
           System.out.println("convert Integer to String: " + yText);
           System.out.println("our Integer: " + y);
           System.out.println("adding Integer = 7 and String = \"7\". The result is a new String = " + y + yText);
       }
    }
    

    ผลลัพธ์คือ:

    
    convert int to String, Java: 5
    our int: 5
    adding int = 5 and String = "5". The result is a new String = 55
    convert Integer to String: 7
    our Integer: 7
    adding Integer = 7 and String = "7". The result is a new String = 77
    

  2. Java แปลง int เป็นสตริงโดยใช้ Integer.toString(int)

    คลาสอ็อบเจกต์เป็นคลาสรูทในภาษาจาวา นั่นหมายถึงคลาส Java ทุกคลาสได้รับการสืบทอดโดยตรงหรือโดยอ้อมจากคลาส Object และเมธอดคลาส Object ทั้งหมดจะพร้อมใช้งานสำหรับคลาส Java ทั้งหมด

    Object มีเมธอดพิเศษ toString() เพื่อแสดงอ็อบเจกต์เป็นสตริง ดังนั้น Java ทุกคลาสจึงสืบทอดวิธีนี้เช่นกัน อย่างไรก็ตาม ความคิดที่ดีคือการแทนที่วิธีนี้ในคลาสของคุณเองเพื่อให้ได้ผลลัพธ์ที่เหมาะสม

    เมธอดของคลาส Integer' toString() ส่งคืนออบเจกต์สตริงที่แสดงพารามิเตอร์ int หรือ Integer ที่ระบุ

    ไวยากรณ์ของมัน:

    
    public static String toString(int i)
    

    เมธอดแปลงอาร์กิวเมนต์ i และส่งกลับเป็นอินสแตนซ์สตริง หากตัวเลขเป็นลบ เครื่องหมายจะถูกเก็บไว้

    ตัวอย่าง:

    
    //  java integer to string using toString method
    
    public class Demo {
       public static void main(String[] args) {
    
           int x = -5;
           //  java convert int to string using Integer.toString
           String xText = Integer.toString(x);
           //  the result output
           System.out.println("convert int to String: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + xText + Integer.toString(x));
    
    
       }
    }
    

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    คุณสามารถใช้เมธอด toString เพื่อแปลง Integer (wrapper type) ได้เช่นกัน

    
    Integer number = -7;
    String numberAsString = Integer.toString(number);
    System.out.println("convert Integer to String: " + numberAsString);
    

    ผลลัพธ์คือ:

    แปลงจำนวนเต็มเป็นสตริง: -7

    คุณสามารถใช้พิเศษInteger.toString method toString(int i, int base)ที่ส่งกลับการแสดงสตริงของตัวเลข i กับฐานฐานและกว่าสตริง ตัวอย่างเช่น

    ไวยากรณ์คือ:

    
    public static String toString(int i, int base)
    

    นี่คือตัวอย่าง:

    
    int a = 255;
    //  binary
    String customString = Integer.toString(a, 2);
    System.out.println(customString);
    

    ผลลัพธ์คือการแสดงไบนารีสตริงของเลขฐานสิบ 255:

    
    11111111
    

  3. แปลง int เป็น String โดยใช้ String.valueOf(int)

    เมธอดString.valueOf(int)ส่งคืนการแสดงสตริงของอาร์กิวเมนต์ int

    ไวยากรณ์ของวิธีการคือ:

    
    public static String valueOf(int i)
    

    นี่คือตัวอย่างของ Java แปลง int เป็น String โดยใช้String.valueOf(int):

    
    public class Demo {
       public static void main(String[] args) {
           int z = -5;
           //  Java int to String converting 
     String zText = String.valueOf(z); 
           //  the result output
           System.out.println("convert int to String: " + zText);
           //  the int output
           System.out.println("our int: " + z);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + zText + z);
       }
    }
    

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    คุณอาจทำเช่นเดียวกันกับ Integer (wrapper type of int):

    
    Integer number = -7;
    String numberAsString = String.valueOf(number);
    System.out.println("convert Integer to String: " + numberAsString);
    

    ผลลัพธ์จะเป็น:

    แปลงจำนวนเต็มเป็นสตริง: -7

  4. แปลงโดยใช้ DecimalFormat

    java.text.DecimalFormatเป็นคลาสที่กำหนดในjava.textแพ็คเกจและคลาสย่อยของNumberFormat. ใช้สำหรับจัดรูปแบบเลขทศนิยมเป็นสตริงที่แสดงตามรูปแบบที่กำหนด เราสามารถใช้กับจำนวนเต็มได้เช่นกัน

    ตัวอย่าง:

    
    import java.text.DecimalFormat;
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 31415;
           DecimalFormat decimalFormat = new DecimalFormat("#");
           String myNumberAsString = decimalFormat.format(myNumber);
           System.out.println(myNumberAsString);
       }
    }
    

    ผลลัพธ์คือ:

    
    31415
    

  5. แปลงโดยใช้ String.format()

    String.format()เป็นอีกวิธีหนึ่งในการแปลง Integer เป็น String Object

    ไวยากรณ์

    
    public static String format(String format, Object... args)
    

    ตัวอย่าง

    
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 35;
           String myNumberAsString = String.format("%d", myNumber);  //  %d converter defines a single decimal integer variable.
           System.out.println(myNumberAsString);
       }
    }
    

    ผลลัพธ์คือ:

    35
    

ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION