Thursday, March 11, 2021

thumbnail

Write a Java program to get the character at the given index within the String.

we can learn to get the character at the given index within the String

Approach Steps 🤔

    • Take given original string 
    • Print  original string for reference 
    • use the charAt() method to find out a character at a given index i.e original.charAt(1);
    • hold it by int type 
    •  while display time typecast int to char 
    • print out the result on the console.

Code bit :


interview Code

Practice Code in LeetCode PlayGround 🥇🥇🥇🥇

Source code :

public class MainClass {
    public static void main(String[] args) throws IOException {
        // take a Given string 
        String original = "Java Code Practice";
        // Print  original string for reference 
       System.out.println( "Given original string :--"+ original+"\n");
        // chatAt method : it is used for find out character at given index
        // hold it by int type 
        int givenIndex = original.charAt(1);
        // while display time type cast to char 
        // Print the character at given index
        System.out.println( "the character at zero index :-- "+(char)givenIndex);
    }
}



Result : 

Result string code


Learnt from this Code Bit :

  • charAt() method: this method is used for getting a character at a given index from the original string. for the above example gave an index at 1 in charAt(1)  method so got the result "a".
  • One more thing "\n" used for the next line 
  • type casting is done int to char so got it from the above code bit. 


Thank you!

:)Sagar R G
 


Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

FullStackDeveloper. Powered by Blogger.