C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs



Home » Java solved programs » Core java programs

Java program to extract words from a given sentence






In this java program, we are going to learn how to extract words from a string? Here, we are reading a string (sentence) and extracting words from it.
Submitted by Preeti Jain, on March 11, 2018

Given a sentence (string) and we have to extract words from a string using java program.

Extract words from a string in java

class ExtractWordFromSentence
{
	public static void main(String args[])
	{
		String str = "My Name Is Preeti Jain";
		String [] words = str.split(" ", 4);

		for (String word : words)
			System.out.println(word);
	}
}

Output

D:\Java Articles>java ExtractWordFromSentence
My
Name
Is
Preeti
Jain






You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.


Comments and Discussions