How to Create String and How to use some important methods of String class. - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline

How to Create String and How to use some important methods of String class.

//String Class Methods
class StringDemo
{
public static void main(String[]args)
{
// String create in three ways
        String s1="The  java";
String s2=new String("I like it");
char arr[]={'s','h','a','s','h','i','k','a','n','t'};
String s3=new String(arr);

// display all the three string
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);


//find the lenght of first string
          System.out.println("Length of s1"+s1.length());
// Concoarated two string
  System.out.println("s1 and s2"+s1.concat(s2));
        // concorated three string with +
                     System.out.println(s1+"from"+s3);

        // if test String s1 start with A
          boolean x=s1.startsWith("A");
        if(x)
          System.out.println("s1 starts with \'A\'");
      else
         System.out.println("s1 does not start with \'A\'");
       // extract substring from s2 stating from 0th char to 6th char
         String p=s2.substring(0,7);
        String q=s3.substring(0,10);
       System.out.println(p+q);
      // convert s1 into uppercase to lowercase
                System.out.println("Upper s1"+s1.toUpperCase());
                  System.out.println("Lower s1"+s1.toLowerCase());

            }
}

About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.