logo

جافا سلسلة القطع ()

ال تقليم فئة سلسلة جافا () تعمل الطريقة على إزالة المسافات البادئة والزائدة. قيمة Unicode لحرف المسافة هي 'u0020'. تتحقق طريقة القطع () في سلسلة Java من قيمة Unicode قبل وبعد السلسلة، إذا كانت موجودة، فستزيل الطريقة المسافات وتعيد السلسلة المحذوفة.

لا تحذف طريقة تقليم السلسلة () المسافات الوسطى.

إمضاء

التوقيع أو بناء الجملة لطريقة تقليم فئة السلسلة () موضح أدناه:

 public String trim() 

عائدات

سلسلة مع حذف المسافات البادئة والزائدة

التنفيذ الداخلي

 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=&apos; hello string &apos;; System.out.println(s1+&apos;javatpoint&apos;);//without trim() System.out.println(s1.trim()+&apos;javatpoint&apos;);//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&apos;s see an example.</p> <p> <strong>FileName:</strong> StringTrimExample2.java</p> <pre> public class StringTrimExample2 { public static void main(String[] args) { String s1 =&apos; hello java string &apos;; 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 = &apos; abc &apos;; if((str.trim()).length() &gt; 0) { System.out.println(&apos;The string contains characters other than white spaces 
&apos;); } else { System.out.println(&apos;The string contains only white spaces 
&apos;); } str = &apos; &apos;; if((str.trim()).length() &gt; 0) { System.out.println(&apos;The string contains characters other than white spaces 
&apos;); } else { System.out.println(&apos;The string contains only white spaces 
&apos;); } } } </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 = &apos; abc &apos;; // 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() + &apos;
&apos;); // no white space present in the string s // therefore, the reference of the s is returned // when the trim() method is invoked String s = &apos;xyz&apos;; 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)>
اختبره الآن

انتاج |

 hello string javatpoint hello stringjavatpoint 

جافا سلسلة تقليم () طريقة المثال 2

يوضح المثال استخدام طريقة القطع (). تقوم هذه الطريقة بإزالة كافة المسافات الزائدة وبالتالي يقلل طول السلسلة أيضًا. دعونا نرى مثالا.

اسم الملف: StringTrimExample2.java

 public class StringTrimExample2 { public static void main(String[] args) { String s1 =&apos; hello java string &apos;; 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() } } 

انتاج |

 22 hello java string 17 hello java string 

جافا سلسلة تقليم () طريقة المثال 3

يمكن استخدام Trim() للتحقق مما إذا كانت السلسلة تحتوي فقط على مسافات بيضاء أم لا. المثال التالي يوضح نفس الشيء.

اسم الملف: TrimExample3.java

 public class TrimExample3 { // main method public static void main(String argvs[]) { String str = &apos; abc &apos;; if((str.trim()).length() &gt; 0) { System.out.println(&apos;The string contains characters other than white spaces 
&apos;); } else { System.out.println(&apos;The string contains only white spaces 
&apos;); } str = &apos; &apos;; if((str.trim()).length() &gt; 0) { System.out.println(&apos;The string contains characters other than white spaces 
&apos;); } else { System.out.println(&apos;The string contains only white spaces 
&apos;); } } } 

انتاج |

 The string contains characters other than white spaces The string contains only white spaces 

جافا سلسلة تقليم () طريقة المثال 4

بما أن السلاسل النصية في Java غير قابلة للتغيير؛ لذلك، عندما يقوم التابع Trim() بمعالجة السلسلة عن طريق قطع المسافات البيضاء، فإنه يقوم بإرجاع سلسلة جديدة. إذا لم تتم المعالجة بواسطة طريقة القطع () فسيتم إرجاع مرجع السلسلة نفسها. لاحظ المثال التالي.

اسم الملف: 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 = &apos; abc &apos;; // 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() + &apos;
&apos;); // no white space present in the string s // therefore, the reference of the s is returned // when the trim() method is invoked String s = &apos;xyz&apos;; String s1 = s.trim(); // the hashcode of s and s1 is the same System.out.println(s.hashCode()); System.out.println(s1.hashCode()); } } 

انتاج |

 The string contains characters other than white spaces The string contains only white spaces