Java String – String methods with examples

Java String

String is sequence of characters.

String is a Java class and is under package – Java.lang.String 

 

String declaration:

  • String str = “hello world 123”; //String literal

str is a String object

  • String MyStr = new String(“My world is great!”); //String new object

 

Watch demo –

 

Sample String declaration –

OutPut :

str – hello world 123

MyStr – My world is great!

 

String constructor – 

OutPut :

hey

String methods

 

charAt –

Returns the character present exactly on specified index

OutPut –

w

 

substring –

OutPut

world 123

world 1

 

concat –

OutPut

hello world 1234567

hello world 123hey

 

Some more String methods – 

OutPut –

6

1

true

false

HELLO WORLD 123

false

true

     123ASDF    

123ASDF

Yello

5

Content posted is based on learning or working experience, Share the post with others if find useful, Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: