logo

طول سلسلة جافا ()

ال طول فئة سلسلة جافا () تجد الطريقة طول السلسلة. طول سلسلة Java هو نفس وحدات كود Unicode للسلسلة.

إمضاء

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

 public int length() 

المحدد من قبل

واجهة CharSequence

عائدات

طول الشخصيات. بمعنى آخر، إجمالي عدد الأحرف الموجودة في السلسلة.

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

 public int length() { return value.length; } 

تستخدم فئة السلسلة داخليًا مصفوفة char[] لتخزين الأحرف. يتم استخدام متغير الطول للمصفوفة للعثور على العدد الإجمالي للعناصر الموجودة في المصفوفة. نظرًا لأن فئة Java String تستخدم مصفوفة char[] داخليًا؛ ولذلك لا يمكن تعريض متغير الطول للعالم الخارجي. ومن ثم، أنشأ مطورو Java طريقة length()، التي تكشف قيمة متغير الطول. يمكن للمرء أيضًا التفكير في طريقة length() باعتبارها طريقة getter() التي توفر قيمة حقل الفئة للمستخدم. يوضح التنفيذ الداخلي بوضوح أن طريقة length() تُرجع قيمة متغير الطول.

مثال على طريقة Java String length()

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

 public class LengthExample{ public static void main(String args[]){ String s1='javatpoint'; String s2='python'; System.out.println('string length is: '+s1.length());//10 is the length of javatpoint string System.out.println('string length is: '+s2.length());//6 is the length of python string }} 
اختبره الآن

انتاج:

string length is: 10 string length is: 6 

Java String length() طريقة المثال 2

نظرًا لأن طريقة length() تعطي إجمالي عدد الأحرف الموجودة في السلسلة؛ لذلك، يمكن للمرء أيضًا التحقق مما إذا كانت السلسلة المحددة فارغة أم لا.

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

 public class LengthExample2 { public static void main(String[] args) { String str = 'Javatpoint'; if(str.length()>0) { System.out.println('String is not empty and length is: '+str.length()); } str = ''; if(str.length()==0) { System.out.println('String is empty now: '+str.length()); } } }

انتاج:

String is not empty and length is: 10 String is empty now: 0 

Java String length() طريقة المثال 3

تُستخدم طريقة length() أيضًا لعكس السلسلة.

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

 class LengthExample3 { // main method public static void main(String argvs[]) { String str = &apos;Welcome To JavaTpoint&apos;; int size = str.length(); System.out.println(&apos;Reverse of the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos; + &apos; is&apos;); for(int i = 0; i <size; i++) { printing in reverse order system.out.print(str.charat(str.length() - i 1)); } < pre> <p> <strong>Output:</strong> </p> <pre> Reverse of the string: &apos;Welcome To JavaTpoint&apos; is tniopTavaJ oT emocleW </pre> <h2>Java String length() Method Example 4</h2> <p>The length() method can also be used to find only the white spaces present in the string. Observe the following example.</p> <p> <strong>FileName:</strong> LengthExample4.java</p> <pre> public class LengthExample4 { // main method public static void main(String argvs[]) { String str = &apos; Welcome To JavaTpoint &apos;; int sizeWithWhiteSpaces = str.length(); System.out.println(&apos;In the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos;); str = str.replace(&apos; &apos;, &apos;&apos;); int sizeWithoutWhiteSpaces = str.length(); // calculating the white spaces int noOfWhieSpaces = sizeWithWhiteSpaces - sizeWithoutWhiteSpaces; System.out.print(&apos;Total number of whitespaces present are: &apos; + noOfWhieSpaces); } } </pre> <p> <strong>Output:</strong> </p> <pre> In the string: &apos; Welcome To JavaTpoint &apos; Total number of whitespaces present are: 4 </pre> <hr></size;>

Java String length() طريقة المثال 4

يمكن أيضًا استخدام طريقة length() للعثور على المسافات البيضاء الموجودة في السلسلة فقط. لاحظ المثال التالي.

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

 public class LengthExample4 { // main method public static void main(String argvs[]) { String str = &apos; Welcome To JavaTpoint &apos;; int sizeWithWhiteSpaces = str.length(); System.out.println(&apos;In the string: &apos; + &apos;&apos;&apos; + str + &apos;&apos;&apos;); str = str.replace(&apos; &apos;, &apos;&apos;); int sizeWithoutWhiteSpaces = str.length(); // calculating the white spaces int noOfWhieSpaces = sizeWithWhiteSpaces - sizeWithoutWhiteSpaces; System.out.print(&apos;Total number of whitespaces present are: &apos; + noOfWhieSpaces); } } 

انتاج:

 In the string: &apos; Welcome To JavaTpoint &apos; Total number of whitespaces present are: 4