FahmidasClassroom

Learn by easy steps

Title

Many built-in string functions exist in Java to perform different types of tasks. The uses of some commonly used string functions and the purpose of using regular expression pattern have been shown in this tutorial.

Example-1: Simple use character and string data

Create a java file with the following code to take a string value from the user and check how many words exists in the input value.

import java.util.*;

public class JavaString1 {

	public static void main(String[] args) {
		
		//Create a scanner object
		Scanner scanner = new Scanner(System.in);
		//Add delimiter
		scanner.useDelimiter(System.getProperty("line.separator"));
		//Define String variable
		String name;
		//Define character variable
		char c;
		//Define number variables
		int len,Counter = 1;
		
		System.out.print("Enter your name: ");
		//Take string input
		name = scanner.next( );
		//Count the length of the string value
		len = name.length( );
        
		//Find out number of words in the string value
		for (int i = 0; i < len; i++) {
			c = name.charAt(i);
			if (c == ' ')
			{
				Counter++;
			} 
		}
	
		//Print string value
		System.out.println("Your name is " + name);
		//Print the number of words
		System.out.println("Your name contains " + Counter + " Parts.");
	}

}

Output:

The following output will appear after executing the above code for the input value, ‘Fahmida Yesmin Chowdhury’.

Example-2: Search particular word in the input values

Create a java file with the following code that will search the word ‘java’ in the input value and the program will be terminated if the user types ‘none’.

import java.util.*;

public class JavaString2 {

	public static void main(String[] args) {
		
		//Create a scanner object
		Scanner scanner = new Scanner(System.in);
		int found = 0;
		String str;
		//Take input until the user types 'none'
		while (true) {
			System.out.print("Enter a programming language name: ");
			str = scanner.next( );
			if (str.equals("none")) 
				break; 
			//Check the string value is 'Java' or not
			else if (str.equalsIgnoreCase("Java"))
				found = 1;
		}
		//Print message based on the input value
		if (found == 1)
		    System.out.println("Java is found in the entries.");	
		else
		    System.out.println("Java is not found in the entries.");	
			
	}

}

Output:

The following output will appear after executing the above code for the input values, ‘PHP’, ‘Java’, ‘Bash’, and ‘none.

The following output will appear after executing the above code for the input values, ‘C++’, ‘Perl’, and ‘none.

Example-3: Use of regular expression in Java

Create a java file with the following code that will validate the input value based on the regular expression pattern.

import java.util.*;

public class JavaString3 {
	
	//Define pattern to verify the input values
	private static final String PATTERN = "CSE[0-9]{2}-[0-9]{4}";
	public static void main(String[] args) {
		
		//Create a scanner object 
		Scanner scanner = new Scanner (System.in);
		//Declare string variables
		String input, msg="";
		//Take input until 'none' is given
		while (true) {
			System.out.print ("Enter the student ID: ");
			input = scanner.next( );
			
			if (input.equals("none")) 
			{
				System.out.println("Terminated."); 
				break;
			}
				
			//Set message based on input value
			if (input.matches(PATTERN)) 
				msg = "Valid ID.";
			else 
				msg = "Invalid ID.";
			//Print the message value
			System.out.println(input + " is " + msg); 
		}
		
	}

}

Output:

The following output will appear after executing the above code for the input values, ‘CSE3’, ‘CSE61-2023’, and ‘none.

Exercise:

Create a java class that will take student’s id, name, admission date, department name, and batch from the user. Write code to validate the data based on the following criterial and print the values when all taken values are valid.

  • The format of the student id will be, ‘year-department-batch-serial’ (Ex: 2023-CSE-65-567).
  • Name must be at least 5 characters long.
  • The format of the admission date will be, ‘dd-mm-yyyy’.
  • Batch value must be within 1 to 99