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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.