String Class Methods in Java: Session 6

The preceding articles introduced a few more string methods that are being used in Java by taking classic examples for a better explanation.

Method: Java String getBytes()


Whenever we need a string in its bytes form, then this method helps to encode the given string into its bytes sequence and return an array of bytes. It has the following three variants:
  • Java String getBytes().
  • Java String getBytes(Charset charset).
  • Java String getBytes(String charsetName).

 

 The getBytes() method encodes the string into bytes sequence by using the default charset and stores it in a new array.

The getBytes(Charset charset) method encodes the string into its byte sequence using a specified charset and returns the bytes array stored in a new array.

The getBytes(String charsetName) method encodes the string into bytes sequence using a specified named charset and returns the bytes array storing it in a new array. It also throws an exception, UnsupportedEncodingException(), if the specified charset is not supported.

Let's see the example for all the variants.

Example

Here in this example, there are outputs for all variants.
  1. import java.io.UnsupportedEncodingException;  
  2. public class GetBytesVariants   
  3. {  
  4.     public static void main(String args[])   
  5.     {  
  6.         System.out.println("Output for getBytes():");  
  7.         System.out.println();  
  8.         int i;  
  9.         String str1 = "Welcome";  
  10.         String str2 = "Knowledge";  
  11.         byte[] ar1 = str1.getBytes();  
  12.         System.out.print("Byte sequence is: "); //print byte sequence of string  
  13.         for (i = 0; i < ar1.length; i++)  
  14.         {  
  15.             System.out.print(ar1[i]);  
  16.         }  
  17.         System.out.println();  
  18.         byte[] ar2 = str2.getBytes();  
  19.         System.out.print("Now Byte sequence is: "); //print byte sequence of string  
  20.         for (byte b: ar2)   
  21.         {  
  22.             System.out.print(b);  
  23.         }  
  24.         System.out.println();  
  25.         System.out.println();  
  26.         System.out.println("Output for getBytes(Charset charset):");  
  27.         System.out.println();  
  28.         try   
  29.         {  
  30.             String str3 = "Programming";  
  31.             System.out.println("str3 is: " + str3); // copy the content of string into byte array  
  32.             byte[] ar3 = str3.getBytes("ASCII");  
  33.             String str4 = new String(ar3); //paste the content of byte array  
  34.             System.out.println("str4 is also: " + str4);  
  35.         }   
  36.         catch (Exception e)   
  37.         {  
  38.             System.out.println(e.toString());  
  39.         }  
  40.         System.out.println();  
  41.         System.out.println("Output for getBytes(String charsetName):");  
  42.         System.out.println();  
  43.         String str5 = "Welcome to 7th session";  
  44.         try   
  45.         {  
  46.             byte[] ar4 = str5.getBytes("UTF-8");  
  47.             System.out.println("UTF-8 character encoding is: " + ar4);  
  48.             byte[] ar5 = str5.getBytes("UTF-16");  
  49.             System.out.println("UTF-16 character encoding is: " + ar5);  
  50.             byte[] ar6 = str5.getBytes("UTF-16BE");  
  51.             System.out.println("UTF-16BE character encoding is: " + ar6);  
  52.             byte[] ar7 = str5.getBytes("UTF-16LE");  
  53.             System.out.println("UTF-16LE character encoding is: " + ar7);  
  54.             byte[] ar8 = str5.getBytes("ISO-8859-1");  
  55.             System.out.println("ISO-8859-1 character encoding is: " + ar8);  
  56.         } catch (UnsupportedEncodingException e)   
  57.         {  
  58.             System.out.println("Unsupported character set");  
  59.         }  
  60.     }  
  61. }  
Output


We can observe the various outputs returned by all the variants.

Method: Java String indexOf()


This method returns the index position of the given character or substring from a specific string. It returns -1 if the character or substring is not found in the specified string.

Note: The index number starts from 0 (zero).

There are the following four variants of this method:
  • Java String indexOf(int ch).
  • Java String indexOf(int ch, int fromIndex).
  • Java String indexOf(String str).
  • Java String indexOf(String str, int fromIndex).

 

The indexOf(int ch) method returns the index position of the first occurrence of a character in a string.

The indexOf(int ch, int fromIndex) method returns the index position of the first occurrence of a character in a specific string, starting from a specified index “fromIndex”.

The indexOf(String str) method returns the index position of the first occurrence of a substring in a specific string.

The indexOf(String str, int fromIndex) method returns the index position of the first occurrence of a substring in a specific string, starting from the specified index “fromIndex”.

Let's see the example for all the variants.

Example

Here is the complete example:
  1. for all the variants.  
  2. public class IndexOfVariants   
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         String str1 = "Levis is a brand name";  
  7.         String str2 = "brand";  
  8.         String str3 = "is";  
  9.         System.out.println("Output for indexOf(int ch): ");  
  10.         System.out.println();  
  11.         System.out.println("Index position of 'a' in str1 is: " + str1.indexOf('a'));  
  12.         System.out.println("Index position of 'n' in str1 is: " + str1.indexOf('n'));  
  13.         System.out.println("Index position of 't' in str1 is: " + str1.indexOf('t'));  
  14.         System.out.println("Index position of 'r' in str2 is: " + str2.indexOf('r'));  
  15.         System.out.println();  
  16.         System.out.println("Output for indexOf(int ch, int fromIndex): ");  
  17.         System.out.println();  
  18.         System.out.println("Index position of 'n' in str1 after 15th character is: " + str1.indexOf('n'15));  
  19.         System.out.println("Index position of 'r' in str1 after 7th character is: " + str1.indexOf('r'7));  
  20.         System.out.println("Index position of 'm' in str1 after 20th character is: " + str1.indexOf('m'20));  
  21.         System.out.println();  
  22.         System.out.println("Output for indexOf(String str): ");  
  23.         System.out.println();  
  24.         System.out.println("Index position of 'brand' in str1 is: " + str1.indexOf(str2));  
  25.         System.out.println("Index position of 'is' in str1 is: " + str1.indexOf(str3));  
  26.         System.out.println("Index position of 'brand' in str1 is: " + str1.indexOf("brand"));  
  27.         System.out.println("Index position of 'is' in str1 is: " + str1.indexOf("is"));  
  28.         System.out.println("Index position of 'not' in str1 is: " + str1.indexOf("not"));  
  29.         System.out.println();  
  30.         System.out.println("Output for indexOf(String str, int fromIndex): ");  
  31.         System.out.println();  
  32.         System.out.println("Index position of 'is' in str1 after 5th character is: " + str1.indexOf("is"5));  
  33.         System.out.println("Index position of 'bran' in str1 after 10th character is: " + str1.indexOf("bran"10));  
  34.         System.out.println("Index position of 'sir' in str1 after 11th character is: " + str1.indexOf("sir"11));  
  35.     }  
  36. }  
Output


Method : Java String lastIndexOf()


This method is also used to return the index position of the last occurrence of the given character or substring from a specific string. It also returns -1 if the character or substring is not found in the specified string.

Note: The index number starts from 0 (zero).

There are again four variants of this method.
  • Java String lastIndexOf(int ch).
  • Java String lastIndexOf(int ch, int fromIndex).
  • Java String lastIndexOf(String str).
  • Java String lastIndexOf(String str, int fromIndex).

 

The lastIndexOf(int ch) method returns the index position of the last occurrence of a character in a string.

The lastIndexOf(int ch, int fromIndex) method returns the index position of the last occurrence of a character in a specific string, beginnig searching backward from the specified index “fromIndex”.

The indexOf(String str) method returns the index position of the last occurrence of a substring in a specific string.

The indexOf(String str, int fromIndex) method returns the index position of last occurrence of a substring in a specific string, beginnig searching backward from the specified index “fromIndex”.

Let's see the example for all the variants.

Example

Here again is the complete example for all the variants.
  1. public class LastIndexOfVariants  
  2. {  
  3.     public static void main(String args[])   
  4.     {  
  5.         String str1 = "Levis is a brand name";  
  6.         String str2 = "brand";  
  7.         String str3 = "is";  
  8.         System.out.println("Output for lastIndexOf(int ch): ");  
  9.         System.out.println();  
  10.         System.out.println("In str1 last 'a' indexed at: " + str1.lastIndexOf('a'));  
  11.         System.out.println("In str1 last 'n' indexed at: " + str1.lastIndexOf('n'));  
  12.         System.out.println("In str1 last 't' indexed at: " + str1.lastIndexOf('t'));  
  13.         System.out.println("In str2 last 'r' indexed at: " + str2.lastIndexOf('r'));  
  14.         System.out.println();  
  15.         System.out.println("Output for lastIndexOf(int ch, int fromIndex): ");  
  16.         System.out.println();  
  17.         System.out.println("Before 12th character in str1 last 'i' indexed at: " + str1.lastIndexOf('i'12));  
  18.         System.out.println("Last 's' whose index <=7: " + str1.lastIndexOf('s'7));  
  19.         System.out.println("Last 'a' whose index <=20: " + str1.lastIndexOf('a'20));  
  20.         System.out.println("Last 'n' whose index <=10: " + str1.lastIndexOf('n'10));  
  21.         System.out.println();  
  22.         System.out.println("Output for lastIndexOf(String str): ");  
  23.         System.out.println();  
  24.         System.out.println("In str1 last occurrence of str2 at: " + str1.lastIndexOf(str2));  
  25.         System.out.println("In str1 last occurrence of str3 at: " + str1.lastIndexOf(str3));  
  26.         System.out.println("In str1 last occurrence of 'brand' at: " + str1.lastIndexOf("brand"));  
  27.         System.out.println("In str1 last occurrence of 'is' at: " + str1.lastIndexOf("is"));  
  28.         System.out.println("In str1 last occurrence of 'not' at: " + str1.lastIndexOf("not"));  
  29.         System.out.println();  
  30.         System.out.println("Output for lastIndexOf(String str, int fromIndex): ");  
  31.         System.out.println();  
  32.         System.out.println("Last occurrence of 'is' before index 3: " + str1.lastIndexOf("is"3));  
  33.         System.out.println("Last occurrence of str2 before index 18: " + str1.lastIndexOf(str2, 18));  
  34.         System.out.println("Last occurrence 'sir' before index 11: " + str1.lastIndexOf("sir"11));  
  35.     }  
  36. }  
Output


Want to switch to next session, then click the link below.
 

Thank you, keep learning and sharing.


Similar Articles