Visualizzazione dei risultati da 1 a 8 su 8

Discussione: stringhe java

  1. #1

    stringhe java

    ragazzi questa è tosta in java.
    Vorrei sapere come posso inserire degli zero in un numero.
    Vi spiego bene:
    io ho un numero che deve essere di 4 cifre.
    Inizialmente potrebbe essere di una cifra tipo 1 così per avere uno standard ci devo metterew davanti 3 cifre così da farlo diventare di 4.
    ex: 0001
    come posso inserire questi zero davanti al numero e soprattutto me la date una mano per il ciclo che effettuerà i controlli se la cifra è di 1 2 3 o 4 cifre.
    Ragazzi datemi una mano
    grazie

  2. #2
    Utente di HTML.it L'avatar di ChReAn
    Registrato dal
    Aug 2002
    Messaggi
    486
    codice:
    /**
      ** Esegue il pad di una stringa S a dimensioni N 
      ** con il carattere C 
      ** da sinistra (True) o da destra (false)
      **/
      public synchronized String paddingString( String s, int n, char c , boolean paddingLeft  ) {
        StringBuffer str = new StringBuffer(s);
        int strLength  = str.length();
        if ( n > 0 && n > strLength ) {
          for ( int i = 0; i <= n ; i ++ ) {
                if ( paddingLeft ) {
                  if ( i < n - strLength ) str.insert( 0, c );
                }
                else {
                  if ( i > strLength ) str.append( c );
                }
          }
        }
        return str.toString();
      }
    Era sufficiente cercare su google java+String+pad... :quipy:
    ChReAn
    -------------------

    Slackware 9.1 powered

  3. #3
    grazie ho capito

  4. #4
    Ehm, versione + performante (nonchè + breve )

    //formattiamo un numero a 4 cifre
    DecimalFormat nf = new DecimalFormat("0000");
    String nuovoNumero = nf.format(numero);
    23-08-2005: Udinese in cémpions lìg
    Questa estate l'ho passata a Tallin

  5. #5
    .....scusate ma il metodo insert....prende al suo interno 2 interi????
    no perch+è mi da errore

  6. #6
    qualcuno sa come funziona bene la insert()????

  7. #7
    la insert vuole un numero e la posizione del carattere utlimo a six che poi sarebbe il primo???
    e poi c che cos'è?

  8. #8
    Direttamente dalla documentazione ufficiale di java 1.4.2

    codice:
    StringBuffer insert(int offset, boolean b) 
              Inserts the string representation of the boolean argument into this string buffer. 
    
     StringBuffer insert(int offset, char c) 
              Inserts the string representation of the char argument into this string buffer. 
    
     StringBuffer insert(int offset, char[] str) 
              Inserts the string representation of the char array argument into this string buffer. 
    
     StringBuffer insert(int index, char[] str, int offset, int len) 
              Inserts the string representation of a subarray of the str array argument into this string buffer. 
    
     StringBuffer insert(int offset, double d) 
              Inserts the string representation of the double argument into this string buffer. 
    
     StringBuffer insert(int offset, float f) 
              Inserts the string representation of the float argument into this string buffer. 
    
     StringBuffer insert(int offset, int i) 
              Inserts the string representation of the second int argument into this string buffer. 
    
     StringBuffer insert(int offset, long l) 
              Inserts the string representation of the long argument into this string buffer. 
    
     StringBuffer insert(int offset, Object obj) 
              Inserts the string representation of the Object argument into this string buffer. 
    
     StringBuffer insert(int offset, String str) 
              Inserts the string into this string buffer.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.