Online-Academy
Look, Read, Understand, Apply

Strings

String

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 = "Academy";
    int length = str.length();
    
  • charAt(): this method is used to get character at given position from the string.
    String str = "Kathmandu";
    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 = "Fewa 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 = "Baneshwor";
    String str2 = "koteshwor";
    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 = "Machapuchare";
    Boolean flag = str.contains("puchare"); //returns true 
    
  • isEmpty(): checks if a string is empty or not, return true is string is empty otherwise returns false
    String str = "Makalu";
    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 = "Makalu";
		System.out.println("Lenght 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:

Lenght of Makalu is: 6
Character at 3rd position of Makalu is: k
Index of character k in Makalu is: 2
Makalu in upper case: MAKALU
Makalu in lower case: makalu
is Makalu equal to Annapurna? false
Does Makalu contains kaltrue
Is Makalu empty? false
Replace a in Makalu with A MAkAlu