CodeGym /جاوا بلاگ /Random-SD /جاوا ۾ int کي String ۾ ڪيئن بدلجي
John Squirrels
سطح
San Francisco

جاوا ۾ int کي String ۾ ڪيئن بدلجي

گروپ ۾ شايع ٿيل
هن آرٽيڪل ۾ اسان انٽ (ابتدائي قسم) ۽ Object ٽائپ (ريپر) انٽيجر کي اسٽرنگ ۾ تبديل ڪرڻ بابت بحث ڪرڻ وارا آهيون. جاوا ۾ ان کي ڪرڻ جا ڪيترائي طريقا آھن.

جاوا ۾ int کي اسٽرنگ ۾ ڪيئن بدلجي

  1. هڪ خالي اسٽرنگ شامل ڪندي تبديل ڪريو.

    int کي String ۾ تبديل ڪرڻ جو آسان طريقو تمام سادو آھي. صرف int يا Integer ۾ شامل ڪريو خالي اسٽرنگ "" ۽ توھان حاصل ڪندا توھان جو int ھڪڙي String جي طور تي. اهو ٿئي ٿو ڇاڪاڻ ته 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. جاوا Integer.toString(int) استعمال ڪندي int کي اسٽرنگ ۾ تبديل ڪريو

    آبجیکٹ ڪلاس جاوا ۾ روٽ ڪلاس آھي. مطلب ته هر جاوا ڪلاس سڌو يا اڻ سڌي طرح آبجیکٹ ڪلاس مان ورثي ۾ ملي ٿو ۽ سڀ آبجیکٹ ڪلاس جا طريقا سڀني جاوا ڪلاسز لاءِ موجود آهن.

    ڪنهن به شئي کي اسٽرنگ طور پيش ڪرڻ لاءِ هڪ خاص طريقو آهي String() ڏانهن. تنهن ڪري، هر جاوا ڪلاس هن طريقي سان پڻ وارث آهي. تنهن هوندي به سٺو خيال اهو آهي ته هن طريقي کي پنهنجي طبقن ۾ اوور رائڊ ڪرڻ لاءِ مناسب نتيجو حاصل ڪرڻ لاءِ.

    Integer class' toString() طريقو هڪ 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 (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. String.valueOf(int) استعمال ڪندي int کي String ۾ تبديل ڪريو

    طريقو String.valueOf(int)int دليل جي اسٽرنگ جي نمائندگي ڪري ٿو.

    طريقي جي نحو آهي:

    public static String valueOf(int i)

    هتي جاوا جو هڪ مثال آهي استعمال ڪندي 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 سان (انٽ جي ريپر قسم):

    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. اهو decimal نمبر کي فارميٽ ڪرڻ لاءِ استعمال ڪيو ويندو آهي هڪ اسٽرنگ تي جيڪو هڪ خاص نموني جي پٺيان ڏيکاريندو آهي. اسان ان کي استعمال ڪري سگھون ٿا Integers لاءِ پڻ.

    مثال:

    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() انٽيجر کي اسٽرنگ آبجیکٹ ۾ تبديل ڪرڻ جو هڪ وڌيڪ طريقو آهي.

    نحو

    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