CodeGym /בלוג Java /Random-HE /כיצד להמיר int למחרוזת ב-Java
John Squirrels
רָמָה
San Francisco

כיצד להמיר int למחרוזת ב-Java

פורסם בקבוצה
במאמר זה נדון בהמרת int (סוג פרימיטיבי) וסוג אובייקט (עטיפה) שלם למחרוזת. ישנן דרכים רבות לעשות זאת ב-Java.

כיצד להמיר int למחרוזת ב-Java

  1. המר על ידי הוספת מחרוזת ריקה.

    הדרך הקלה ביותר להמיר int ל-String היא פשוטה מאוד. פשוט הוסף ל-int או למספר שלם מחרוזת ריקה "" ותקבל את ה-int שלך כמחרוזת. זה קורה בגלל שהוספת int ו-String נותנת לך מחרוזת חדשה. זה אומר שאם יש לך int x = 5, פשוט תגדיר x + ""ותקבל את המחרוזת החדשה שלך.

    הנה דוגמא:

    //  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. זה אומר שכל מחלקת Java עוברת בירושה ישירות או בעקיפין ממחלקת Object וכל שיטות מחלקות Object זמינות עבור כל מחלקות Java.

    לאובייקט יש שיטה מיוחדת toString() כדי לייצג כל אובייקט כמחרוזת. אז כל מחלקה של Java יורשת גם את השיטה הזו. עם זאת, הרעיון הטוב הוא לעקוף את השיטה הזו בשיעורים שלך כדי לקבל תוצאה מתאימה.

    השיטה toString() של המחלקה Integer מחזירה אובייקט String המייצג את הפרמטר 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 number = -7;
    String numberAsString = Integer.toString(number);
    System.out.println("convert Integer to String: " + numberAsString);

    התוצאה היא:

    המרת מספר שלם למחרוזת: -7

    אתה יכול להשתמש ב-special Integer.toString method toString(int i, int base)שמחזיר ייצוג מחרוזת של המספר i עם בסיס הבסיס ולא ל-String. לדוגמה

    התחביר הוא:

    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.valueOf(int)

    השיטה String.valueOf(int)מחזירה את ייצוג המחרוזת של הארגומנט int.

    התחביר של השיטה הוא:

    public static String valueOf(int i)

    הנה דוגמה להמרת Java int למחרוזת באמצעות 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

    אתה יכול לעשות את אותו הדבר עם מספר שלם (סוג עטיפה של 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() היא דרך אחת נוספת להמיר מספר שלם ל-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