TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java First Words in String

Use a for-loop to get the first several words in a string. Count spaces and use the substring method.
First words. A string contains many words. These are separated by spaces. To create a caption of the text, we sometimes need to get the first words from the string.Strings
With a loop, and a char-testing algorithm, we can extract the first N words from a string. We use substring() to extract the first part from the text.For
An example. Here is the firstWords method. This method receives two arguments: the string we want to get the first words from, and the number of words to extract. It uses a for-loop.

CharAt: We use the charAt method to count spaces. We decrement the "words" count when a space is encountered.

charAt

Substring: When no more words are needed, we return a string of the word up to the current space. This substring contains the first words.

Substring
Java program that gets first words from string public class Program { public static String firstWords(String input, int words) { for (int i = 0; i < input.length(); i++) { // When a space is encountered, reduce words remaining by 1. if (input.charAt(i) == ' ') { words--; } // If no more words remaining, return a substring. if (words == 0) { return input.substring(0, i); } } // Error case. return ""; } public static void main(String[] args) { String value = "One two three four five."; System.out.println(value); // Test firstWords on the first 3 and 4 words. String words3 = firstWords(value, 3); System.out.println(words3); String words4 = firstWords(value, 4); System.out.println(words4); } } Output One two three four five. One two three One two three four
Some issues. The method does not handle an invalid number of words. If you specify a million words and there are not that many words, an empty string will be returned.IsEmpty
A learning exercise. When learning a programming language, writing many simple methods like this one is helpful. Many things can be accomplished with a simple char-testing loop.
A summary. This method extracts the first words from a string. This can be used to create brief summaries of text (or captions for images). First words are often sufficient for labels.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf