Sunday, 24 July 2016

java string class

OBJECTIVE

Study Java String class & Java String Buffer.

THEORY

Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are objects.
Java platform provide the String class to create and manipulate strings.

Creating Strings:

Most direct way to create a string is to write:
String greeting = "Hello world!";    //aamirjamil
Whenever it encounters a string literal in your code, the compiler creates a String object with its value in this case, "Hello world!'.
As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:
public class StringDemo{
 
   public static void main(String args[]){     //made by aamirjamil
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
}



AND THE OUTPUT WILL BE:

hello.
 

Note: String class is immutable, so that once it's created a String object can't be changed. If there's a necessity to make alot of modifications to Strings of characters then you should use String Buffer and String Builder Classes.              

String Methods:

Here are the list methods supported by String class:




PROGRAM 1: 

A PALINDROME is a word which has SAME SPELLING whether it is read from Left to Right or from Right to Left. Example: MOM, DAD, DEED, PEEP and NOON. Other words which are not PALINDROME are HELLO, DOOR and FEET. Write a program that can take a String as user input in Capital Letters and then Print YES as Output if the Input is a PALINDROME otherwise NO.
 
SOURCE CODE:
import java.util.Scanner;
public class Palindrome{  // made by aamirjmail

       public static void main (String[] args){
              Scanner input=new Scanner(System.in);
              String s1 = new String();
              String s2= new String();
              String s3= new String();
              System.out.println("Enter Your PALINDROME : ");
              s1=input.nextLine();
              for(int i=0; i<s1.length(); i++) {
                     s3 = s1.substring(((s1.length())-(i+1)),((s1.length())-i));  
                     s2 = s2+s3;
              }
              if(s1.equals(s2))
                     System.out.println("YES ITS A PALINDROME");    
              else
                     System.out.println("NO IT IS NOT A PALINDROME");   

       }
} 

OUTPUT:










PROGRAM 2: 


Write a program that extracts username & domain information from Email address.
For eg:, if the email address is "user@my.com", your program will print
User name = user              
Domain = my                             
Extension = com 

SOURCE CODE:
import java.util.Scanner;
public class email {   //by aamirjamil
      public static void main(String[] args) {
            Scanner enter = new Scanner(System.in);
            String email = new String();
            email = enter.nextLine();
            String uname, ext, dom;
            uname = email.substring(0,(email.indexOf('@')));
            System.out.print("Username : "+uname);
            dom = email.substring((email.indexOf('@')+1),(email.indexOf('.')));
            System.out.print("\nDomain : "+dom);
            ext = email.substring((email.indexOf('.')+1),email.length());
            System.out.print("\nExtension : "+ext);
      }
}


OUTPUT:



No comments:

Post a Comment