Java - String Charat Example - How To Become Outset Too End Characters

The charAt(int index) method of java.lang.String class tin post away live used to recollect a graphic symbol from given index. The method returns a character, yous tin post away encounter its supply type is char. The index starts from zero too ranges to length() - 1 where length() supply the length of String. It's similar to an array where the start chemical ingredient is stored at zeroth index too the final chemical ingredient is stored at length -1 index because String is naught but backed past times a graphic symbol array. If the value of the index is invalid i.e. it's lower than null or negative or higher than length - 1 too thus this method throws an IndexOutOfBoundsException. In this article, I'll present yous a distich of instance of how to utilisation the charAt() method to recollect a graphic symbol from String inwards Java.


1. chartAt() instance amongst valid index

If yous desire to acquire a graphic symbol or alphabetic character from the 4th seat inwards given String yous tin post away telephone telephone the charAt() method every bit below:

char 4th = "Avengers".charAt(3); // seat iii holds 4th character

Similarly to recollect the 5th graphic symbol yous tin post away overstep 4 to the charAt method every bit shown below:

char fifth = "Avengers".chartAt(4);

Above code volition supply 5th graphic symbol from the String.

For example, if sample String is "Avengers" too thus charAt(3) volition supply alphabetic character "n", the 4th graphic symbol too charAt(4) volition supply alphabetic character "g", the 5th character.



2. Getting First Character from String

If yous desire the start alphabetic character from a given String too thus simply telephone telephone the charAt(0) because the start graphic symbol stays at the zeroth seat inwards String. For example, if given String is "Avenger" too thus charAt(0) volition supply alphabetic character "A", the start alphabetic character every bit shown below:

char first = "Avengers".charAt(0); System.out.println(first)  Output A

You tin post away encounter the output is alphabetic character A, the first character from the given String "Avengers".


3. Getting the Last Character from String

If yous desire to recollect the final graphic symbol from String too thus yous tin post away telephone telephone charAt(length -1) where length is the length of given String. For example, if given String is "Avengers" too thus "Avengers".charAt(7) volition supply alphabetic character "s" the final letter, which is from the 7th seat because the length of String is 8.

String sample = "Avengers"; String last = sample.charAt(sample.length() -1);  System.out.println(last); Output s

You tin post away encounter that the final graphic symbol returned is the alphabetic character "s", which is from the 7th position. If yous desire to larn to a greater extent than well-nigh unlike String methods e.g. length etc, See IndexOutOfBoundException.

Following code volition throw IndexOufOfBoundsException, yous tin post away simply re-create too run this inwards your Eclipse or IntelliJ IDEA code too encounter the output:

"Avengers".charAt(8); // index equal to length of String "Avengers".charAt(-1) // negative index "Avengers".charAt(9); // index greter than length of String


5. Complete Java Program

If yous desire to play amongst this method, hither is simply about sample code to start with. You tin post away modify the String or modify the index to figure out how to utilisation charAt() method, what does it returns too when it throws an error:

package tool;  /**  * Influenza A virus subtype H5N1 unproblematic Java Program to demonstrate how to utilisation charAt() method of String   * cast to recollect start too final character.  */  public class Hello {    public static void main(String[] args) {      // sample String     String sample = "Avengers";      // Retrieve start graphic symbol from String     char firstLetter = sample.charAt(0);      System.out.println("first alphabetic character from String: " + sample + " is : "                                 + firstLetter);      // Retrieving final graphic symbol from String     char lastLetter = sample.charAt(0);     System.out.println("last alphabetic character of String: " + sample + " is : "                                  + lastLetter);      // invalid index volition throw IndexOutOfBoundsException     sample.charAt(-1);     sample.charAt(sample.length());    }  } Output: the first alphabetic character from String: Avengers is: Influenza A virus subtype H5N1 last alphabetic character of String: Avengers is: Influenza A virus subtype H5N1  Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(String.java:658) at tool.Hello.main(Hello.java:26)

You tin post away encounter that StringIndexOutOfBoundsException, which is similar ArrayIndexOutOfBoundException a subclass of IndexOutOfBoundsException is thrown because of an invalid index.

If yous are non familiar amongst Errors too Exception inwards Java. I advise yous become through The Complete Java Masterclass to larn it. It's really substitution concept too a skillful noesis of them it is required to write robust production character Java code.

class tin post away live used to recollect a graphic symbol from given index Java - String charAt Example - How to acquire start too final characters



6. Important Points

Now that yous know what does charAt(int index) function create too how it works, let's revise simply about of import points:

1) The charAt() component is defined inwards the java.lang.String class

2) The charAt() component accepts an integer which is the index of graphic symbol yous desire to recollect from String.

3) If an index is invalid i.e. less than null too higher than the length of String - 1 too thus it throws IndexOutOfBoundsException.

4) You tin post away overstep index null if yous desire start graphic symbol too length() - 1 to recollect the final character.

5) To acquire all the graphic symbol inwards the array, utilisation the toCharArray() too to retrieve, a flow of characters from String inwards Java 8, simply utilisation the chars() method, which is solely available since Java 8 inwards String class. You tin post away farther see The Complete Java Masterclass to larn to a greater extent than well-nigh novel methods added inwards JDK 8, 9, too 10 on String class.

6) Instead of retrieving a graphic symbol from a seat if yous desire reverse i.e. to know the seat of a given graphic symbol too thus yous tin post away utilisation the indexOf(char letter) too lastIndexOf(char letter) method which returns the seat of given graphic symbol or -1 if given graphic symbol is non flora inwards String.

That's all well-nigh how to utilisation the charAt() component from String cast inwards Java. As explained inwards this article, the charAt() method tin post away live used to recollect an private graphic symbol from given String. You simply withdraw to overstep the valid index. For example, if yous desire the start graphic symbol from String too thus simply telephone telephone String.charAt(0) because index starts at null inwards String.

Further Learning
The Complete Java Masterclass
The String API Documentation (Java SE 10)

Thanks for reading this article. If yous similar this article too thus delight part amongst your friends too colleagues. If yous receive got whatever questions or feedback too thus delight drib a note.

0 Response to "Java - String Charat Example - How To Become Outset Too End Characters"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel