logo

Java virknes apakšvirkne()

The Java virknes klases apakšvirkne() metode atgriež daļu no virknes.

Mēs nododam beginIndex un endIndex numura pozīcijas Java apakšvirknes metodē, kur beginIndex ir iekļaujošs un endIndex ir ekskluzīvs. Citiem vārdiem sakot, beginIndex sākas ar 0, bet endIndex sākas no 1.

Java virknē ir divu veidu apakšvirkņu metodes.

Paraksts

 public String substring(int startIndex) // type - 1 and public String substring(int startIndex, int endIndex) // type - 2 

Ja nenorādīsim endIndex, metode atgriezīs visas rakstzīmes no startIndex.

Parametri

startIndex : sākuma indekss ir iekļaujošs

beiguIndekss : beigu indekss ir ekskluzīvs

Atgriežas

norādītā virkne

Izņēmuma metieni

StringIndexOutOfBoundsException tiek izmests, ja ir izpildīts kāds no šiem nosacījumiem.

  • ja sākuma indekss ir negatīva vērtība
  • beigu indekss ir zemāks par sākuma indeksu.
  • Sākuma vai beigu indekss ir lielāks par virknē esošo rakstzīmju kopskaitu.

Iekšējās ieviešanas apakšvirkne (int beginIndex)

 public String substring(int beginIndex) { if (beginIndex <0) { throw new stringindexoutofboundsexception(beginindex); } int sublen="value.length" - beginindex; if (sublen < 0) stringindexoutofboundsexception(sublen); return (beginindex="=" ? this : string(value, beginindex, sublen); pre> <h3>Internal implementation substring(int beginIndex, int endIndex) </h3> <pre> public String substring(int beginIndex, int endIndex) { if (beginIndex value.length) { throw new StringIndexOutOfBoundsException(endIndex); } int subLen = endIndex - beginIndex; if (subLen <0) { throw new stringindexoutofboundsexception(sublen); } return ((beginindex="=" 0) && (endindex="=" value.length)) ? this : string(value, beginindex, sublen); < pre> <h2>Java String substring() method example</h2> <p> <strong>FileName:</strong> SubstringExample.java</p> <pre> public class SubstringExample{ public static void main(String args[]){ String s1=&apos;javatpoint&apos;; System.out.println(s1.substring(2,4));//returns va System.out.println(s1.substring(2));//returns vatpoint }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre>va vatpoint </pre> <h2>Java String substring() Method Example 2</h2> <p> <strong>FileName:</strong> SubstringExample2.java</p> <pre> public class SubstringExample2 { public static void main(String[] args) { String s1=&apos;Javatpoint&apos;; String substr = s1.substring(0); // Starts with 0 and goes to end System.out.println(substr); String substr2 = s1.substring(5,10); // Starts from 5 and goes to 10 System.out.println(substr2); String substr3 = s1.substring(5,15); // Returns Exception } } </pre> <p> <strong>Output:</strong> </p> <pre> Javatpoint point Exception in thread &apos;main&apos; java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10 </pre> <h2>Applications of substring() Method</h2> <p>1) The substring() method can be used to do some prefix or suffix extraction. For example, we can have a list of names, and it is required to filter out names with surname as &apos;singh&apos;. The following program shows the same.</p> <p> <strong>FileName:</strong> SubstringExample3.java</p> <pre> public class SubstringExample3 { // main method public static void main(String argvs[]) { String str[] = { &apos;Praveen Kumar&apos;, &apos;Yuvraj Singh&apos;, &apos;Harbhajan Singh&apos;, &apos;Gurjit Singh&apos;, &apos;Virat Kohli&apos;, &apos;Rohit Sharma&apos;, &apos;Sandeep Singh&apos;, &apos;Milkha Singh&apos; }; String surName = &apos;Singh&apos;; int surNameSize = surName.length(); int size = str.length; for(int j = 0; j <size; j++) { int length="str[j].length();" extracting the surname string substr="str[j].substring(length" - surnamesize); checks whether is equal to 'singh' or not if(substr.equals(surname)) system.out.println(str[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> Yuvraj Singh Harbhajan Singh Gurjit Singh Sandeep Singh Milkha Singh </pre> <p>2) The substring() method can also be used to check whether a string is a palindrome or not.</p> <p> <strong>FileName:</strong> SubstringExample4.java</p> <pre> public class SubstringExample4 { public boolean isPalindrome(String str) { int size = str.length(); // handling the base case if(size == 0 || size == 1) { // an empty string // or a string of only one character // is always a palindrome return true; } String f = str.substring(0, 1); String l = str.substring(size - 1); // comparing first and the last character of the string if(l.equals(f)) { // recursively finding the solution using the substring() method // reducing the number of characters of the by 2 for the next recursion return isPalindrome(str.substring(1, size - 1)); } return false; } // main method public static void main(String argvs[]) { // instantiating the class SubstringExample4 SubstringExample4 obj = new SubstringExample4(); String str[] = { &apos;madam&apos;, &apos;rock&apos;, &apos;eye&apos;, &apos;noon&apos;, &apos;kill&apos; }; int size = str.length; for(int j = 0; j <size; j++) { if(obj.ispalindrome(str[j])) system.out.println(str[j] + ' is a palindrome.'); } else not < pre> <p> <strong>Output:</strong> </p> <pre> madam is a palindrome. rock is not a palindrome. eye is a palindrome. noon is a palindrome. kill is not a palindrome. </pre> <hr></size;></pre></size;></pre></0)></pre></0)>
Izmēģiniet to tagad

Izvade:

va vatpoint 

Java String substring() metodes 2. piemērs

Faila nosaukums: ApakšvirkneExample2.java

 public class SubstringExample2 { public static void main(String[] args) { String s1=&apos;Javatpoint&apos;; String substr = s1.substring(0); // Starts with 0 and goes to end System.out.println(substr); String substr2 = s1.substring(5,10); // Starts from 5 and goes to 10 System.out.println(substr2); String substr3 = s1.substring(5,15); // Returns Exception } } 

Izvade:

 Javatpoint point Exception in thread &apos;main&apos; java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10 

Apakšstring() metodes pielietojumi

1) Apakšvirknes () metodi var izmantot, lai veiktu prefiksu vai sufiksu izvilkšanu. Piemēram, mums var būt vārdu saraksts, un ir jāfiltrē vārdi, kuru uzvārds ir 'singh'. Nākamā programma parāda to pašu.

Faila nosaukums: ApakšvirkneExample3.java

 public class SubstringExample3 { // main method public static void main(String argvs[]) { String str[] = { &apos;Praveen Kumar&apos;, &apos;Yuvraj Singh&apos;, &apos;Harbhajan Singh&apos;, &apos;Gurjit Singh&apos;, &apos;Virat Kohli&apos;, &apos;Rohit Sharma&apos;, &apos;Sandeep Singh&apos;, &apos;Milkha Singh&apos; }; String surName = &apos;Singh&apos;; int surNameSize = surName.length(); int size = str.length; for(int j = 0; j <size; j++) { int length="str[j].length();" extracting the surname string substr="str[j].substring(length" - surnamesize); checks whether is equal to \'singh\' or not if(substr.equals(surname)) system.out.println(str[j]); } < pre> <p> <strong>Output:</strong> </p> <pre> Yuvraj Singh Harbhajan Singh Gurjit Singh Sandeep Singh Milkha Singh </pre> <p>2) The substring() method can also be used to check whether a string is a palindrome or not.</p> <p> <strong>FileName:</strong> SubstringExample4.java</p> <pre> public class SubstringExample4 { public boolean isPalindrome(String str) { int size = str.length(); // handling the base case if(size == 0 || size == 1) { // an empty string // or a string of only one character // is always a palindrome return true; } String f = str.substring(0, 1); String l = str.substring(size - 1); // comparing first and the last character of the string if(l.equals(f)) { // recursively finding the solution using the substring() method // reducing the number of characters of the by 2 for the next recursion return isPalindrome(str.substring(1, size - 1)); } return false; } // main method public static void main(String argvs[]) { // instantiating the class SubstringExample4 SubstringExample4 obj = new SubstringExample4(); String str[] = { &apos;madam&apos;, &apos;rock&apos;, &apos;eye&apos;, &apos;noon&apos;, &apos;kill&apos; }; int size = str.length; for(int j = 0; j <size; j++) { if(obj.ispalindrome(str[j])) system.out.println(str[j] + \' is a palindrome.\'); } else not < pre> <p> <strong>Output:</strong> </p> <pre> madam is a palindrome. rock is not a palindrome. eye is a palindrome. noon is a palindrome. kill is not a palindrome. </pre> <hr></size;></pre></size;>

2) Substring() metodi var izmantot arī, lai pārbaudītu, vai virkne ir palindroms vai nē.

Faila nosaukums: ApakšvirkneExample4.java

 public class SubstringExample4 { public boolean isPalindrome(String str) { int size = str.length(); // handling the base case if(size == 0 || size == 1) { // an empty string // or a string of only one character // is always a palindrome return true; } String f = str.substring(0, 1); String l = str.substring(size - 1); // comparing first and the last character of the string if(l.equals(f)) { // recursively finding the solution using the substring() method // reducing the number of characters of the by 2 for the next recursion return isPalindrome(str.substring(1, size - 1)); } return false; } // main method public static void main(String argvs[]) { // instantiating the class SubstringExample4 SubstringExample4 obj = new SubstringExample4(); String str[] = { &apos;madam&apos;, &apos;rock&apos;, &apos;eye&apos;, &apos;noon&apos;, &apos;kill&apos; }; int size = str.length; for(int j = 0; j <size; j++) { if(obj.ispalindrome(str[j])) system.out.println(str[j] + \\' is a palindrome.\\'); } else not < pre> <p> <strong>Output:</strong> </p> <pre> madam is a palindrome. rock is not a palindrome. eye is a palindrome. noon is a palindrome. kill is not a palindrome. </pre> <hr></size;>