JAVA program for string method trim()

JAVA program for string method trim()

This JAVA program is for string method trim().

String method trim() removes blank spaces from the start and end of a string.

For example  if string str = ‘    hello world   ‘, then str.trim() will remove blank space from start and end of the string thus giving us string as ‘hello world’.

Dry Run of the Program

Take input ‘str’.Let us take input str=’    CODEDOST   ‘

str.trim = ‘CODEDOST‘ (blank spaces removed)

Program

import java.util.*;

class trim
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter the string");
		String str = sc.nextLine();
		
		System.out.println("On using .trim()="+str.trim());
	}
}

Output

Share Me!