IL funzione di rifinitura in Java String è una funzione incorporata che elimina gli spazi iniziali e finali. Il valore Unicode del carattere spazio è 'u0020'. Il metodo trim() in Java controlla questo valore Unicode prima e dopo la stringa, se esiste rimuove gli spazi e restituisce la stringa omessa. Anche il trim() in Java aiuta a tagliare i caratteri.
Nota: Il metodo trim() non elimina gli spazi intermedi.
mockito ogni volta

Firma del metodo
public String trim()>
Parametri
- Il metodo trim() non accetta parametri.
Tipo di reso
- Il tipo restituito dal metodo trim() è Corda . Restituisce la stringa omessa senza spazi iniziali e finali.
Di seguito sono riportati degli esempi per mostrare il funzionamento della stringa ordinare() metodo in Java.
Esempi di Trim() in Java
Esempio 1:
La funzione trim per rimuovere gli spazi bianchi dalla sinistra e dalla destra della stringa può essere utilizzata quando vogliamo unire più stringhe insieme.
Di seguito è riportata l'implementazione dell'argomento:
Giava
// Java program to demonstrate working> // of java string trim() method> import> java.io.*;> > // Driver Class> class> GFG {> >// Main Function> >public> static> void> main (String[] args) {> > >// Three strings declared> >String x=>'geeks '>;> >String y=>'for '>;> >String z=>'geeks'>;> > >// Printing without trim function> >System.out.println(x+y+z);> > >// Using trim function to get result> >System.out.println(x.trim()+y.trim()+z.trim());> >}> }> |
>
>Produzione
sommatore pieno
geeks for geeks geeksforgeeks>
Esempio 2:
Dopo aver utilizzato la funzione trim, restituisce la stringa anziché apportare modifiche alla stringa originale.
Di seguito è riportata l'implementazione dell'argomento precedente:
Giava
come rinominare una directory linux
// Java program to demonstrate working> // of java string trim() method> > class> Gfg {> > >// driver code> >public> static> void> main(String args[])> >{> >// trims the trailing and leading spaces> >String s> >=>' geeks for geeks has all java functions to read '>;> > >// Printing String after removing the whitespaces> >// from the string> >System.out.println(s.trim());> > >// Printing string to observe> >System.out.println(s);> >}> }> |
>
>Produzione
geeks for geeks has all java functions to read geeks for geeks has all java functions to read>
Complessità temporale: SU)
Spazio ausiliario: O(1)
Esempio 3:
Durante l'utilizzo della funzione trim, otteniamo due stringhe originali e la stringa restituita, entrambe sono diverse nel caso in cui stiamo rimuovendo gli spazi bianchi dalla stringa originale.
Di seguito è riportata l'implementazione dell'argomento precedente:
Giava
errore di esecuzione
// Java program to demonstrate working> // of java string trim() method> import> java.io.*;> > // Driver Class> class> GFG {> >// Main Function> >public> static> void> main(String[] args)> >{> >// String declared> >String s1 =>' Geeks For Geeks '>;> > >// Before Trim() method> >System.out.println(>'Before Trim() - '>);> >System.out.println(>'String - '> + s1);> >System.out.println(>'Length - '> + s1.length());> > >// applying trim() method on string s1> >String s2 = s1.trim();> > >// After Trim() method> >System.out.println(>'
After Trim() - '>);> >System.out.println(>'String - '> + s2);> >System.out.println(>'Length - '> + s2.length());> > >// Comparing both the strings> >if> (s1 == s2) {> >System.out.println(>'
Equal'>);> >}> >else> {> >System.out.println(>'
Not Equal'>);> >}> >}> }> |
>
>Produzione
Before Trim() - String - Geeks For Geeks Length - 21 After Trim() - String - Geeks For Geeks Length - 15 Not Equal>
Complessità temporale: SU)
Spazio ausiliario: O(1)
convertire una stringa in intero