logo

Dĺžka reťazca Java ()

The Dĺžka triedy Java String() metóda nájde dĺžku reťazca. Dĺžka reťazca Java je rovnaká ako kódové jednotky Unicode reťazca.

Podpis

Podpis metódy string length() je uvedený nižšie:

 public int length() 

Určené používateľom

Rozhranie CharSequence

Návraty

Dĺžka znakov. Inými slovami, celkový počet znakov prítomných v reťazci.

Interná implementácia

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

Trieda String interne používa pole char[] na ukladanie znakov. Premenná dĺžky poľa sa používa na nájdenie celkového počtu prvkov prítomných v poli. Keďže trieda Java String používa toto pole char[] interne; preto premenná dĺžky nemôže byť vystavená vonkajšiemu svetu. Preto vývojári Java vytvorili metódu length(), ktorá odhaľuje hodnotu premennej dĺžky. Metódu length() možno považovať aj za metódu getter(), ktorá používateľovi poskytuje hodnotu poľa triedy. Interná implementácia jasne ukazuje, že metóda length() vracia hodnotu premennej dĺžky.

Príklad metódy Java String length().

Názov súboru: 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 }} 
Vyskúšajte to

Výkon:

string length is: 10 string length is: 6 

Príklad metódy Java String length() 2

Keďže metóda length() udáva celkový počet znakov prítomných v reťazci; preto je možné tiež skontrolovať, či je daný reťazec prázdny alebo nie.

Názov súboru: 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()); } } }

Výkon:

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

Príklad metódy Java String length() 3

Metóda length() sa používa aj na obrátenie reťazca.

Názov súboru: 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;>

Príklad metódy Java String length() 4

Metódu length() možno použiť aj na nájdenie iba bielych miest v reťazci. Všimnite si nasledujúci príklad.

Názov súboru: 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); } } 

Výkon:

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