The Java String razred trim() metoda odpravlja presledke na začetku in na koncu. Vrednost presledka Unicode je 'u0020'. Metoda trim() v nizu Java preveri to vrednost Unicode pred in za nizom; če obstaja, potem metoda odstrani presledke in vrne izpuščeni niz.
Metoda string trim() ne izpusti srednjih presledkov.
Podpis
Podpis ali sintaksa metode trim() razreda String je podana spodaj:
public String trim()
Vračila
niz z izpuščenimi začetnimi in končnimi presledki
Notranja izvedba
public String trim() { int len = value.length; int st = 0; char[] val = value; /* avoid getfield opcode */ while ((st <len) && (val[st] <=" " )) { st++; } while ((st len) (val[len - 1] 0) || (len value.length)) ? substring(st, : this; pre> <h2>Java String trim() Method Example</h2> <p> <strong>FileName:</strong> StringTrimExample.java</p> <pre> public class StringTrimExample{ public static void main(String args[]){ String s1=' hello string '; System.out.println(s1+'javatpoint');//without trim() System.out.println(s1.trim()+'javatpoint');//with trim() }} </pre> <span> Test it Now </span> <p> <strong>Output</strong> </p> <pre> hello string javatpoint hello stringjavatpoint </pre> <h2>Java String trim() Method Example 2</h2> <p>The example demonstrates the use of the trim() method. This method removes all the trailing spaces so the length of the string also reduces. Let's see an example.</p> <p> <strong>FileName:</strong> StringTrimExample2.java</p> <pre> public class StringTrimExample2 { public static void main(String[] args) { String s1 =' hello java string '; System.out.println(s1.length()); System.out.println(s1); //Without trim() String tr = s1.trim(); System.out.println(tr.length()); System.out.println(tr); //With trim() } } </pre> <p> <strong>Output</strong> </p> <pre> 22 hello java string 17 hello java string </pre> <h2>Java String trim() Method Example 3</h2> <p>The trim() can be used to check whether the string only contains white spaces or not. The following example shows the same.</p> <p> <strong>FileName:</strong> TrimExample3.java</p> <pre> public class TrimExample3 { // main method public static void main(String argvs[]) { String str = ' abc '; if((str.trim()).length() > 0) { System.out.println('The string contains characters other than white spaces '); } else { System.out.println('The string contains only white spaces '); } str = ' '; if((str.trim()).length() > 0) { System.out.println('The string contains characters other than white spaces '); } else { System.out.println('The string contains only white spaces '); } } } </pre> <p> <strong>Output</strong> </p> <pre> The string contains characters other than white spaces The string contains only white spaces </pre> <h2>Java String trim() Method Example 4</h2> <p>Since strings in Java are immutable; therefore, when the trim() method manipulates the string by trimming the whitespaces, it returns a new string. If the manipulation is not done by the trim() method, then the reference of the same string is returned. Observe the following example.</p> <p> <strong>FileName:</strong> TrimExample4.java</p> <pre> public class TrimExample4 { // main method public static void main(String argvs[]) { // the string contains white spaces // therefore, trimming the spaces leads to the // generation of new string String str = ' abc '; // str1 stores a new string String str1 = str.trim(); // the hashcode of str and str1 is different System.out.println(str.hashCode()); System.out.println(str1.hashCode() + ' '); // no white space present in the string s // therefore, the reference of the s is returned // when the trim() method is invoked String s = 'xyz'; String s1 = s.trim(); // the hashcode of s and s1 is the same System.out.println(s.hashCode()); System.out.println(s1.hashCode()); } } </pre> <p> <strong>Output</strong> </p> <pre> The string contains characters other than white spaces The string contains only white spaces </pre> <hr></len)>Preizkusite zdaj
Izhod
hello string javatpoint hello stringjavatpoint
2. primer metode Java String trim().
Primer prikazuje uporabo metode trim(). Ta metoda odstrani vse končne presledke, tako da se zmanjša tudi dolžina niza. Poglejmo primer.
Ime datoteke: StringTrimExample2.java
public class StringTrimExample2 { public static void main(String[] args) { String s1 =' hello java string '; System.out.println(s1.length()); System.out.println(s1); //Without trim() String tr = s1.trim(); System.out.println(tr.length()); System.out.println(tr); //With trim() } }
Izhod
22 hello java string 17 hello java string
3. primer metode Java String trim().
Trim() lahko uporabite za preverjanje, ali niz vsebuje samo prazne presledke ali ne. Naslednji primer prikazuje isto.
Ime datoteke: TrimExample3.java
public class TrimExample3 { // main method public static void main(String argvs[]) { String str = ' abc '; if((str.trim()).length() > 0) { System.out.println('The string contains characters other than white spaces '); } else { System.out.println('The string contains only white spaces '); } str = ' '; if((str.trim()).length() > 0) { System.out.println('The string contains characters other than white spaces '); } else { System.out.println('The string contains only white spaces '); } } }
Izhod
The string contains characters other than white spaces The string contains only white spaces
4. primer metode Java String trim().
Ker so nizi v Javi nespremenljivi; torej, ko metoda trim() manipulira z nizom z obrezovanjem presledkov, vrne nov niz. Če manipulacija ni izvedena z metodo trim(), se vrne referenca istega niza. Upoštevajte naslednji primer.
Ime datoteke: TrimExample4.java
public class TrimExample4 { // main method public static void main(String argvs[]) { // the string contains white spaces // therefore, trimming the spaces leads to the // generation of new string String str = ' abc '; // str1 stores a new string String str1 = str.trim(); // the hashcode of str and str1 is different System.out.println(str.hashCode()); System.out.println(str1.hashCode() + ' '); // no white space present in the string s // therefore, the reference of the s is returned // when the trim() method is invoked String s = 'xyz'; String s1 = s.trim(); // the hashcode of s and s1 is the same System.out.println(s.hashCode()); System.out.println(s1.hashCode()); } }
Izhod
The string contains characters other than white spaces The string contains only white spaces