Navigate back to the homepage

how to get last character of string in react native?

Infinitbility
React Native
August 16th, 2022 · 1 min read
how to get last character of string in react native?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get last character of string in react native, use substring() method to extracts last character from string. You have to only pass starting and ending postion of string so for last character pass text.length - 1 as parameter it will return last character of string.

Let’s see short example to use substring() to get last character from string.

1text.substring(text.length - 1);

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

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

React native get last character of string example using substring()

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

1import React, { useState, useEffect } from "react";
2import { Text, View } from 'react-native';
3export default function App() {
4 const [str, setStr] = useState("Infinitbility");
5
6 useEffect(() => {
7 setStr(str.substring(str.length - 1))
8 }, []);
9
10 return (
11 <View>
12 <Text>{str}</Text>
13 </View>
14 );
15}

Output

React native get last character of string example
React native get last character of string example

Try it yourself

React native get last character of string example using index

Here, we will take string state with some data and use index method to get last char of string.

1import React, { useState, useEffect } from "react";
2import { Text, View } from 'react-native';
3
4export default function App() {
5 const [str, setStr] = useState("Infinitbility");
6
7 useEffect(() => {
8 setStr(str[str.length - 1])
9 }, []);
10
11 return (
12 <View>
13 <Text>{str}</Text>
14 </View>
15 );
16}

Output

React native get last character of string example
React native get last character of string example

Try it yourself

React native get last character of string example using charAt()

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

1import React, { useState, useEffect } from "react";
2import { Text, View } from 'react-native';
3
4export default function App() {
5 const [str, setStr] = useState("Infinitbility");
6
7 useEffect(() => {
8 setStr(str.charAt(str.length - 1))
9 }, []);
10
11 return (
12 <View>
13 <Text>{str}</Text>
14 </View>
15 );
16}

Output

React native get last character of string example
React native get last character of string example

Try it yourself

I hope it helps you, All the best 👍.

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 character of string in typescript?

how to get last character of string in typescript?

An example of get last character of string in typescript with the step-by-step guide.

August 16th, 2022 · 1 min read
how to get first two character of string in javascript?

how to get first two character of string in javascript?

An example of get first two character of a string in javascript with the step-by-step guide.

August 15th, 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]