top of page

Cheat Sheet of String for Programming in Java

Updated: Apr 15, 2020

let's use Java Library for String in Online Programming Contest.




We are providing Java defined String methods for typecasting, extraction of character, comparison of strings, search sub-strings and modify strings.


Character Arrays and String


Character arrays and Strings required many times in online programming, their is very good relationship between them .Let's explore ....



1. Character Array to String Conversion


class Main{
    public static void main(String args[]){
   
     char[] stringArray = {'V','I','V','E','K','K','U','M','A','R'};
     String requiredString = new String(stringArray);
     System.out.println(requiredString);
     //OUTPUT :VIVEKKUMAR
     
     
     int startIndex=2;
     int endIndex=5;
     String subString = new String(stringArray,startIndex,endIndex);
     System.out.println(subString);
     //OUTPUT :VEKKU
     
     byte[] ascii={65,66,67,68};
     String asciiToString = new String(ascii);
     System.out.println(asciiToString);
     //OUTPUT :ABCD
    
    }
}


2. String Length and data type Conversion to String


import java.util.*;
class Main{
    public static void main(String args[]){
   
     String virusName = new String("Covid-19");
     int virusNameLength = virusName.length();
     System.out.println(virusNameLength);
     //OUTPUT: 8
     
     String VirusForm = "Corona Virus Disease";
     String virusNameWithFullForm = virusName + " - " + VirusForm;
     System.out.println(virusNameWithFullForm);
     //OUTPUT: Covid-19 - Corona Virus Disease
     
     int originYearOfVirus = 2019;
     virusNameWithFullForm += String.valueOf(originYearOfVirus);
     System.out.println(virusNameWithFullForm);
     //OUTPUT: Covid-19 - Corona Virus Disease2019
    }
}

Here, we have to focus on valueOf method . We used here for primitive type viz int.

Actually while using concatenation operator '+' or using valueOf method java internally typecast it to String but in case of user defined type we have to use

tostring method in java which return String format of fields of your class or which user want to return.


3. Extraction of Characters and Character Array from Strings


import java.util.*;
class Main{
    public static void main(String args[]){
   
   String statement= "IndiaismyCountry";
   int index  = 2;
   char character = statement.charAt(index);//can use in loops
  
   System.out.println(character);
   //OUTPUT: d
   
   int startIndex=0;
   int endIndex=5;
   int arraySize = endIndex-startIndex;
   
   char[] subString = new char[arraySize];
   statement.getChars(startIndex,endIndex,subString,0);
  
   System.out.println(subString);
   //OUTPUT: India
   
   char[] arrayOfString = statement.toCharArray();
   
   System.out.println(arrayOfString);
   //OUTPUT: IndiaismyCountry
    }
}

4. Comparison between Strings


import java.util.*;
class Main{
    public static void main(String args[]){
   
   String fruit="apple";
   String fruitName="Apple";
   boolean check=fruit.equals(fruitName);
   System.out.println(check);
   //OUTPUT :false
   
   String updatedFruitName ="apple";
   boolean checkAgain = fruit.equals(updatedFruitName);
   System.out.println(checkAgain);
   //OUTPUT :true
   
   boolean equality = fruit.equalsIgnoreCase(fruitName);
   System.out.println(equality);
   //OUTPUT: true
   
   String substring = "pple";
   boolean checkSubstring =fruit.regionMatches(true,1,substring,0,4);
   System.out.println(checkSubstring);
   //OUTPUT: true
   
   boolean isStartingWith= fruit.startsWith("ple",2);
   System.out.println(isStartingWith);
   //OUTPUT: true
   
   boolean isEndsWith =fruit.endsWith("Le");
   System.out.println(isEndsWith);
   //OUTPUT: false
   
   //sort in descending order
   String list[]={"Now","is","the","time","for","all","good","men"};
   for(int i=0;i<list.length;i++){
       for(int j=i+1;j<list.length;j++){
           if(list[i].compareToIgnoreCase(list[j])<0){
               String t=list[j];
               list[j]=list[i];
               list[i]=t;
           }
       }
       System.out.print(list[i]+" ");
     }
     //OUTPUT: time the Now men is good for all
     
    }
}

Note: campareTo is used to compare between two string .


5. Modify a String


As we all know that string object is immutable in java so, during every modification new object is created to stop this you can use StringBuffer of StringBuilder class which is mutable .



import java.util.*;
class Main{
    public static void main(String args[]){
   
  
     String statement ="This is Placebook Blog";
     String substring= statement.substring(8,17);
     System.out.println(substring);
     //OUTPUT: Placebook
     
     String first ="Tom";
     String second="Jerry";
     String addition=first.concat(second);
     System.out.println(addition);
     //OUTPUT: TomJerry
     
    String lowerCase=statement.toLowerCase();
    String upperCase=statement.toUpperCase();
    
    System.out.println(lowerCase);
    //OUTPUT: this is placebook Blog
    
    System.out.println(upperCase);
    //OUTPUT: THIS IS PLACEBOOK BLOG
     
    }
}





Comments


bottom of page