Java Basic String Manipulation OOPs Concept In Java



        

import java.util.*;
public class StringMethod
{
	public static void main(String[] args) {
		
		//String Method (immutable)
		String str = "Rajomay";
		
		System.out.println(str.length());  //for string length
                                            // str.length is used for string array length
		System.out.println(str.toUpperCase());
		System.out.println(str.toLowerCase());
		
		System.out.println(str.substring(1));   //a
		System.out.println(str.substring(0,1)); //R
		
		System.out.println(str.replace('a','z'));     //replace char in string
		System.out.println(str.replaceAll("ma","z"));  //replace string
		
		System.out.println("  Rajoshree Tanmay  ".trim());   //remove white space from front and end
		
		System.out.println(str.charAt(2));
		System.out.println(str.indexOf('j'));
		
		System.out.println(str.contains("jo")); //true
		System.out.println(str.endsWith("may")); //true
		System.out.println(str.startsWith("Raj")); //true
		System.out.println(str.isEmpty()); //false
		
		//concatination
		System.out.println(str.concat("Rajoshree"));
		String s1 = str.concat("TANMAY");
		System.out.println(s1);
		
		//equals
		String str1 = "RAjoshreeTanmay";
		String str2 = "rajoshreetanmay";
		System.out.println(str1.equals(str2)); //false
		System.out.println(str1.equalsIgnoreCase(str2)); //true
		System.out.println(str1.compareTo("RAjoshreeTanmay")); //0
		
		//Conversion in String
		String str0 = "RAJOMAY";
		char a[] = str0.toCharArray();      //string to array
		
		int a1 = 1;
		String s = String.valueOf(a1);      //any DataTYPE to string 
        //String s = Integer.toString(a1);
		System.out.println(s);
		
		//String array, For Each, & Split()
		System.out.println("===========================");
		String str9 = "Rajoshree and Tanmay";
		String[] str10 = str9.split("\\s"); //split on the basis of white space "\\s"
		for(String w:str10){
		    System.out.println(w);
		}
		System.out.println("===========================");
//=================================================================================================//


		//String Method (Mutable)->[StringBuffer] Thread Safe, Store in Heap
	    //                       ->[StringBuilder] Not Thread Safe, Store in Heap   
	    
	       // Reverse Method
	       StringBuffer buffer = new StringBuffer(str);
	       //System.out.println(buffer.reverse());
	       buffer.reverse();
	       System.out.println(buffer);
	              //or
	       StringBuilder builder = new StringBuilder(str);
	       System.out.println(builder.reverse());
	       
	       // Append and testing concat
	       String string = "Tanmay";
    	       //string.concat("Rajoshree"); //Does not concat because string is immutable  
    	       System.out.println(string);  
    	       string = string.concat("Rajoshree"); // concate
    	       System.out.println(string);
    	       
    	       StringBuffer b = new StringBuffer(string);
    	       b.append("Rajoshree");
    	       System.out.println(b);

//=====================================================================================

            //Remove Duplicate Char in String
            String str18 ="aaaaAshuSouravDDipanjan";
            	String str19 = new String();
            
            	for(int i =0 ;i < str18.length();i++){
            	    char ch = str18.charAt(i);
            	    if(str19.indexOf(ch) == -1){
            	        str19+=ch;
            	    }
            	}
            System.out.println(str19);
            
//===============================================================================
	    //sort the string
    	    String sortStr = "xaetarda";
    	    char[] array = sortStr.toCharArray();
    	    Arrays.sort(array);   //import java.util.Arrays;
    	    sortStr = String.valueOf(array);
    		System.out.println("Sorted String: "+sortStr);
            
//================================================================
       //sum of String
       int sum = 0;
       String str100 = "rajoMay".toUpperCase();
       char[] p = str100.toCharArray();
       for(char i:p){
           int val = (int)i;
           sum+=(val-64);
       }
       System.out.println(sum); //83
                   
            
	}
}