Open In App

JavaScript Program to Extract First Word from a String

Last Updated : 14 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we have to extract the first word from the input string in JavaScript language. Below is an example for better understanding.

Examples:

Input: Geeks for Geeks
Output: Geeks
Input: I Love India
Output: I


Examples of Extracting First Word from a String

1. Using split() Method

In this approach, we are using the split() method to extract the first word from the input string. The split() method in JS is used to split the string into the array of substrings and from this substring, we extract the first word which is stored at the 0th index.

Syntax

string.split(separator, limit);


Example: In this example, we will be extracting the first word from a string using the split() Method.

JavaScript
let str = "Geeks For Geeks";
let res = str.split(' ')[0];
console.log(res);

Output
Geeks


2.Using substring() and indexOf() Methods

In this approach, we are using substring() and indexOf() methods to extract the first word from the string. The substring() method here is used to extract a substring from the beginning of the input string up to that index. The indexOf() method is used to find the index of the first space character in the input string.

Syntax

string.substring(string.indexOf(searchValue, startIndex), endIndex);


Example: In this example, we will be extracting the first word from a string using substring() and indexOf() Methods.


JavaScript
let str = "Geeks For Geeks";
let index = str.indexOf(' ');
let res = 
    str.substring(0, index !== -1 ? 
        index : str.length);
console.log(res); 

Output
Geeks


3.Using Regular Expression

In this approach, we are using the regular expression/^\S+/‘ that is used with the match() method to find and print the dist sequence of non-space characters at the beginning of the input string. If a match is found then it assigns that match to the output variable else it assigns the empty string.

Syntax

string.match(expression);


Example: In this example, we will be extracting the first word from a string using regular expression.

JavaScript
let str = "Geeks For Geeks";
let match = str.match(/^\S+/);
let res = match ? match[0] : '';
console.log(res);

Output
Geeks


4.Using slice() and indexOf() Methods

In this approach, the slice() method and indexOf() methods are used to extract the first word from the input string. Here, the indexOf() method is used to locate the index of the first space character in the input string, and the slice() method is used to extract the desired substring from the start of the input string tup to the index computed by indexOf() method.

Syntax

string.slice(0, string.indexOf(' ')) || string;

Example: In this example, we will be extracting the first word from a string using slice() and indexOf() Methods.

JavaScript
let str = "Geeks For Geeks";
let index = str.indexOf(' ');
let res = 
    index !== -1 ? 
        str.slice(0, index) : str;
console.log(res);

Output
Geeks


5.Using for loop

Extract the first word from a string using a for loop. Iterate through characters until encountering a space. Concatenate characters to form the first word.

Syntax

for (statement 1 ; statement 2 ; statement 3){
code here...
}


Example: In this example we are following above mentioned apporach.

JavaScript
function extractFirstWord(str) {
    let word = "";
    for (let i = 0; i < str.length; i++) {
        // Append characters to the word until a space is encountered
        if (str[i] !== " ") {
            word += str[i];
        } else {
            // Break the loop when a space is encountered
            break;
        }
    }
    return word;
}

// Example usage
const string = "Geeks for Geeks";
const firstWord = extractFirstWord(string);
console.log(firstWord); //

Output
Geeks





Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads