Navigate back to the homepage

how to get string after last slash in javascript?

Infinitbility
JavaScript
August 29th, 2022 · 1 min read
how to get string after last slash in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get string after last slash in javascript, use split() method with pop() method it will return those string which after then your last slash or which you used to split the string.

Let’s see short example to use split() method with pop() method to get string after last slash.

1string.split("/").pop();

Today, I’m going to show you How do I get string after last slash in javascript, as above mentioned, I’m going to use the above-mentioned split() method with pop() method.

Let’s start today’s tutorial how do you get string after last slash in javascript?

Here, I will show get string after last slash examples in javascript, typescript, react js and react native.

Javascript get string after last slash example

Here, we will take string variable with some data and use split() with pop() method to get string after last slash in javascript.

1let string = "file/newuser.jpg";
2
3let afterLifeStr = string.split("/").pop();
4
5console.log(afterLifeStr)
6
7// Output
8// 'newuser.jpg'

Output

Typescript get string after last slash example

Here, we will take string variable with some data and use split() with pop() method to get string after last slash in typescript.

1let string: string = "file/newuser.jpg";
2
3let afterLifeStr: string = string.split("/").pop();
4
5console.log(afterLifeStr)
6
7// Output
8// 'newuser.jpg'

Output

React js get string after last slash example

Here, we will take string state with some data and use split() with pop() method to get string after last slash in react js.

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [str, setStr] = useState("file/newuser.jpg");
4
5 useEffect(() => {
6 let afterLifeStr = str.split("/").pop();
7 console.log(afterLifeStr)
8 }, []);
9
10 return (
11 <div className="App">
12 {str.split("/").pop()}
13 </div>
14 );
15}

Output

React native get string after last slash example

Here, we will take string state with some data and use split() with pop() method to get string after last slash in react native.

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

Output

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 string after dot in javascript?

how to get string after dot in javascript?

An example of get string after dot in javascript with the step-by-step guide.

August 28th, 2022 · 1 min read
how to get string after space in javascript?

how to get string after space in javascript?

An example of get string after space in javascript with the step-by-step guide.

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