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.
- import java.io.UnsupportedEncodingException;
- public class GetBytesVariants
- {
- public static void main(String args[])
- {
- System.out.println("Output for getBytes():");
- System.out.println();
- int i;
- String str1 = "Welcome";
- String str2 = "Knowledge";
- byte[] ar1 = str1.getBytes();
- System.out.print("Byte sequence is: ");
- for (i = 0; i < ar1.length; i++)
- {
- System.out.print(ar1[i]);
- }
- System.out.println();
- byte[] ar2 = str2.getBytes();
- System.out.print("Now Byte sequence is: ");
- for (byte b: ar2)
- {
- System.out.print(b);
- }
- System.out.println();
- System.out.println();
- System.out.println("Output for getBytes(Charset charset):");
- System.out.println();
- try
- {
- String str3 = "Programming";
- System.out.println("str3 is: " + str3);
- byte[] ar3 = str3.getBytes("ASCII");
- String str4 = new String(ar3);
- System.out.println("str4 is also: " + str4);
- }
- catch (Exception e)
- {
- System.out.println(e.toString());
- }
- System.out.println();
- System.out.println("Output for getBytes(String charsetName):");
- System.out.println();
- String str5 = "Welcome to 7th session";
- try
- {
- byte[] ar4 = str5.getBytes("UTF-8");
- System.out.println("UTF-8 character encoding is: " + ar4);
- byte[] ar5 = str5.getBytes("UTF-16");
- System.out.println("UTF-16 character encoding is: " + ar5);
- byte[] ar6 = str5.getBytes("UTF-16BE");
- System.out.println("UTF-16BE character encoding is: " + ar6);
- byte[] ar7 = str5.getBytes("UTF-16LE");
- System.out.println("UTF-16LE character encoding is: " + ar7);
- byte[] ar8 = str5.getBytes("ISO-8859-1");
- System.out.println("ISO-8859-1 character encoding is: " + ar8);
- } catch (UnsupportedEncodingException e)
- {
- System.out.println("Unsupported character set");
- }
- }
- }
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 specific 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:
- for all the variants.
- public class IndexOfVariants
- {
- public static void main(String args[])
- {
- String str1 = "Levis is a brand name";
- String str2 = "brand";
- String str3 = "is";
- System.out.println("Output for indexOf(int ch): ");
- System.out.println();
- System.out.println("Index position of 'a' in str1 is: " + str1.indexOf('a'));
- System.out.println("Index position of 'n' in str1 is: " + str1.indexOf('n'));
- System.out.println("Index position of 't' in str1 is: " + str1.indexOf('t'));
- System.out.println("Index position of 'r' in str2 is: " + str2.indexOf('r'));
- System.out.println();
- System.out.println("Output for indexOf(int ch, int fromIndex): ");
- System.out.println();
- System.out.println("Index position of 'n' in str1 after 15th character is: " + str1.indexOf('n', 15));
- System.out.println("Index position of 'r' in str1 after 7th character is: " + str1.indexOf('r', 7));
- System.out.println("Index position of 'm' in str1 after 20th character is: " + str1.indexOf('m', 20));
- System.out.println();
- System.out.println("Output for indexOf(String str): ");
- System.out.println();
- System.out.println("Index position of 'brand' in str1 is: " + str1.indexOf(str2));
- System.out.println("Index position of 'is' in str1 is: " + str1.indexOf(str3));
- System.out.println("Index position of 'brand' in str1 is: " + str1.indexOf("brand"));
- System.out.println("Index position of 'is' in str1 is: " + str1.indexOf("is"));
- System.out.println("Index position of 'not' in str1 is: " + str1.indexOf("not"));
- System.out.println();
- System.out.println("Output for indexOf(String str, int fromIndex): ");
- System.out.println();
- System.out.println("Index position of 'is' in str1 after 5th character is: " + str1.indexOf("is", 5));
- System.out.println("Index position of 'bran' in str1 after 10th character is: " + str1.indexOf("bran", 10));
- System.out.println("Index position of 'sir' in str1 after 11th character is: " + str1.indexOf("sir", 11));
- }
- }
OutputMethod : 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 specific 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.
- public class LastIndexOfVariants
- {
- public static void main(String args[])
- {
- String str1 = "Levis is a brand name";
- String str2 = "brand";
- String str3 = "is";
- System.out.println("Output for lastIndexOf(int ch): ");
- System.out.println();
- System.out.println("In str1 last 'a' indexed at: " + str1.lastIndexOf('a'));
- System.out.println("In str1 last 'n' indexed at: " + str1.lastIndexOf('n'));
- System.out.println("In str1 last 't' indexed at: " + str1.lastIndexOf('t'));
- System.out.println("In str2 last 'r' indexed at: " + str2.lastIndexOf('r'));
- System.out.println();
- System.out.println("Output for lastIndexOf(int ch, int fromIndex): ");
- System.out.println();
- System.out.println("Before 12th character in str1 last 'i' indexed at: " + str1.lastIndexOf('i', 12));
- System.out.println("Last 's' whose index <=7: " + str1.lastIndexOf('s', 7));
- System.out.println("Last 'a' whose index <=20: " + str1.lastIndexOf('a', 20));
- System.out.println("Last 'n' whose index <=10: " + str1.lastIndexOf('n', 10));
- System.out.println();
- System.out.println("Output for lastIndexOf(String str): ");
- System.out.println();
- System.out.println("In str1 last occurrence of str2 at: " + str1.lastIndexOf(str2));
- System.out.println("In str1 last occurrence of str3 at: " + str1.lastIndexOf(str3));
- System.out.println("In str1 last occurrence of 'brand' at: " + str1.lastIndexOf("brand"));
- System.out.println("In str1 last occurrence of 'is' at: " + str1.lastIndexOf("is"));
- System.out.println("In str1 last occurrence of 'not' at: " + str1.lastIndexOf("not"));
- System.out.println();
- System.out.println("Output for lastIndexOf(String str, int fromIndex): ");
- System.out.println();
- System.out.println("Last occurrence of 'is' before index 3: " + str1.lastIndexOf("is", 3));
- System.out.println("Last occurrence of str2 before index 18: " + str1.lastIndexOf(str2, 18));
- System.out.println("Last occurrence 'sir' before index 11: " + str1.lastIndexOf("sir", 11));
- }
- }
OutputWant to switch to next session, then click the link below.
Thank you, keep learning and sharing.