Navigate back to the homepage

how to get last word of string in react native?

Infinitbility
React Native
August 22nd, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get last word of string in react native, use split(" ") method with strArr.length - 1 index value it will return last word from string. You have to only pass " " in split() method.

Let’s see short example to use split(" ") with with strArr.length - 1 index to get last word from string.

1let strArr = string.split(" ");
2strArr[strArr.length - 1];

Today, I’m going to show you How do I get last word of string in react native, as above mentioned, I’m going to use the above-mentioned split() method.

Let’s start today’s tutorial how do you get last word of string in react native?

React native get last word of string example

Here, we will take string state with some data and use split() method to get last word of string.

1import React, { useState, useEffect } from "react";
2import { Text, View } from 'react-native';
3
4export default function App() {
5 const [str, setStr] = useState("Welcome to infinitbility");
6
7 useEffect(() => {
8 console.log(getLastWord(str))
9 }, []);
10
11 const getLastWord = (str) => {
12 let strArr = str.split(" ");
13
14 if(strArr.length > 0){
15 return strArr[strArr.length - 1];
16 }
17
18 return "";
19 }
20
21 return (
22 <View>
23 <Text>{getLastWord(str)}</Text>
24 </View>
25 );
26}

TIP: Before acceing any array value using index check length greater than 0 or not

I hope it helps you, All the best 👍.

Output

React native get last word of string example
React native get last word of string example

Try it yourself

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to get last word of string in typeScript?

An example of get last word of a string in typeScript with the step-by-step guide.

August 21st, 2022 · 1 min read

how to get last word of string in javascript?

An example of get last word of a string in javascript with the step-by-step guide.

August 20th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]