logo image

Basic String operations in Java



This is a simple Java program shows you the basic String operations that you can perform on Strings

public class BasicStringOperations {

	public static void main(String[] args) {

		String firstname = "firstName";
		String lastname = "lastName";
		
		// add two Strings
		System.out.println(firstname + " " + lastname);
		
		// Changing the case
		System.out.println(firstname.toUpperCase() + " " + lastname.toUpperCase());
		
		// Changing the case
		System.out.println(firstname.toLowerCase() + " " + lastname.toLowerCase());
		
		// adding two strings with another String in middle
		System.out.println(firstname + ", " + " " + lastname);
		
		// Searching the index of a character - Positive case 
		System.out.println(firstname.indexOf("i"));
		
		// Searching the index of a character - Negative case
		System.out.println(firstname.indexOf("q"));
		
		// Checking whether or not the String has given character in it
		System.out.println(firstname.contains("q"));
		
		// Getting the character at 4th position
		System.out.println(firstname.charAt(4));
	}
}

Output

firstName lastName
FIRSTNAME LASTNAME
firstname lastname
firstName,  lastName
1
-1
false
t