CodeGym/Blog Java/Poland/Zamiana string na int w Java
Autor
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

Zamiana string na int w Java

Opublikowano w grupie Poland
W tym artykule omówimy konwersję int (prymitywny typ) i Integer (klasy opakowującej otrzymanej z klasy Object) na String i na odwrót. Java umożliwia osiągnięcie tego na wiele sposobów.

Konwersja typu int na String w Javie

  1. Konwertuj, dodając pusty ciąg.

    Konwertowanie int na String jest bardzo proste. Po prostu dodaj "" (pusty ciąg) do int lub Integer, a otrzymasz swój int jako String. Dzieje się tak, ponieważ dodanie int i String daje nowy String. Innymi słowy, jeżeli masz int x = 5, po prostu piszesz x + "", a otrzymasz swój nowy String.

    Oto przykład:

    //  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);
       }
    }

    Drukowane będzie:

    
    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. Konwertuj int na String przy użyciu Javy Integer.toString(int)

    Klasa Object jest pierwotną klasą w języku Java. Oznacza to, że każda klasa Java bezpośrednio lub pośrednio dziedziczy klasę Object, a wszystkie metody klasy Object są dostępne we wszystkich klasach Java.

    Object ma metodę toString (), która reprezentuje dowolny obiekt jako ciąg. Tak więc każda klasa Javy również dziedziczy tę metodę. To powiedziawszy, dobrym pomysłem jest nadpisywanie tej metody we własnych klasach, aby uzyskać pożądane wyniki.

    Metoda toString () klasy Integer zwraca obiekt String reprezentujący przekazany argument int lub Integer.

    Składnia:

    public static String toString(int i)

    Metoda dokonuje konwersji i i zwraca instancję typu String. Jeśli liczba jest ujemna, znak minus zostanie zawarty w ciągu.

    Przykład:

    //  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
    

    Możesz również użyć metody toString, aby konwertować Integer (typ wrappera).

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

    Drukowane będzie:

    Konwertuj Integer na String: -7

    Możesz użyć wariantu metody Integer.toString method toString(int i, int base), która zwraca ciąg znaków reprezentujący liczbę i o podstawie base.

    Składnia:

    public static String toString(int i, int base)

    Oto przykład:

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

    Wydrukiem jest String zawierający binarną reprezentację liczby dziesiętnej 255:

    
    11111111
    

  3. Konwertuj int na String przy użyciu String.valueOf(int) Javy

    Metoda String.valueOf(int) zwraca ciąg reprezentujący argument int.

    Składnią metody jest:

    public static String valueOf(int i)

    Oto przykład konwertowania języka Java int do String przy użyciu metody Javy 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
    

    Możesz zrobić to samo z Integer (typ wrappera int):

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

    Wynikiem będzie:

    Konwertuj Integer na String: -7

  4. Konwertuj za pomocą DecimalFormat

    java.text.DecimalFormat jest klasą zdefiniowaną w pakiecie java.text i jest podklasą NumberFormat. Służy do formatowania liczby dziesiętnej jako ciągu według określonego wzoru. Możemy go użyć również do pracy z instancjami Integer.

    Przykład:

    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);
       }
    }

    Drukowane będzie:

    
    31415
    

  5. Konwertuj za pomocą String.format()

    String.format() to jeszcze jeden sposób na konwertowanie Integer na obiekt String.

    Składnia:

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

    Przykład

    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);
       }
    }

    Drukowane będzie:

    35
    

Zamiana string na int w Java - 1
Komentarze
  • Popularne
  • Najnowsze
  • Najstarsze
Musisz się zalogować, aby dodać komentarz
Ta strona nie ma jeszcze żadnych komentarzy