String Handling in Java

String Handling in Java

exploreJava logo

String is a collection of characters enclosed in double quotes. In Java, Strings are objects with several methods defined with it; methods for finding length of the string, index of specific character, string within the string, getting substring, getting character at specific position of the string, comparing string, converting case of string and many more.

String methods

  • length(): this method is used to find length, number of characters, of a string.
    String str = "Programming";
    int length = str.length();
  • charAt(): this method is used to get character at given position from the string.
    String str = "Language";
    char ch = str.charAt(4);
    ch is contain m;
  • indexOf():this method returns position of specified character in the string if the character is present otherwise returns -1.
    String str = "Ama Dablam";
    int pos = str.indexOf('D');
    returns position of D in str
  • toUpperCase(): this method will change case of letters of string to Upper case.
    String str = "Rara Lake";
    str.toLowerCase(str);
    returns fewa lake
  • toLowerCase(): this method will change case of letters of string to Lower case.
    String str = "Fewa lake";
    str.toUpperCase(str);
    returns FEWA LAKE
  • equals(): compares two strings, true is returned if strings are equal otherwise false is returned.
    String str = "Old Baneshwor";
    String str2 = "Santinagar";
    Boolean flag = str.equals(str2); // return false
  • contains: this method checks if a string contains given sequence of characters. It returns true if sequence of characters is found otherwise returns false.
    String str = "America";
    Boolean flag = str.contains("rica"); //returns true
  • isEmpty(): checks if a string is empty or not, return true is string is empty otherwise returns false
    String str = "Himalayan";
    Boolean flag = str.isEmpty(); //returns false
  • replace(): searches for a specified character and if found replaces with specified character.
    String str = "hold";
    str.replace('o','e'); // returns held
  • split: divides a string into array
    
    
  • trim: removes white-space before and after string
    String str = " hello Everest ";
    str.trim();

Example: String and its methods

class Strings{
public static void main(String[] op){
String str = "Himalaya";
System.out.println("Length of "+str+" is: "+str.length());
System.out.println("Character at 3rd position of "+str+" is: "+str.charAt(2));
System.out.println("Index of character k in "+str+" is: "+str.indexOf('k'));
System.out.println(str+" in upper case: "+str.toUpperCase());
System.out.println(str+" in lower case: "+str.toLowerCase());
System.out.println("is "+str+" equal to Annapurna? "+str.equals("Annapurna"));
System.out.println("Does "+str+" contains kal"+str.contains("kal"));
System.out.println("Is "+str +" empty? "+str.isEmpty());
System.out.println("Replace a in "+str+" with A "+str.replace('a','A'));
}
}

output:

Length of Himalaya is: 8
Character at 3rd position of Himalaya is: m
Index of character k in Himalaya is: -1
Himalaya in upper case: HIMALAYA
Himalaya in lower case: himalaya
is Himalaya equal to Annapurna? false
Does Himalaya contains kalfalse
Is Himalaya empty? false
Replace a in Himalaya with A HimAlAyA


Switch Case in Jaba

Introduction to Java Programming

Post a Comment

Previous Post Next Post