Navigate back to the homepage

how to replace string in react native?

React Native
June 23rd, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To replace a string in react native, just use the replace() method for replacing the first occurrence and replaceAll() for replacing all occurrences.

Like the following example, we can replace a string with any string or empty ( blank ).

1"Hello Infinitbility".replace("Hello", "Hi"); // 'Hi Infinitbility'
2"Hello Infinitbility".replaceAll("i", "-"); // 'Hello Inf-n-tb-l-ty'

Today, I’m going to show you How do i replace strings in react native based on their occurrence, as above mentioned here, I’m going to use the replace() and replaceAll() method for replacing strings.

Let’s start today’s tutorial how do you replace a string in react native?

In this example, we will replace a string in a text component ( in a render method ).

Let’s write code…

1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
4export default function App() {
5 const [string, setString] = useState("Hello Infinitbility");
7 return (
8 <View style={styles.container}>
9 <Text>{string.replace("Hello", "Hi")}</Text>
10 <Text>{string.replaceAll("i", "-")}</Text>
11 </View>
12 );
15const styles = StyleSheet.create({
16 container: {
17 flex: 1,
18 justifyContent: 'center',
19 backgroundColor: '#ecf0f1',
20 padding: 8,
21 },
22 paragraph: {
23 margin: 24,
24 fontSize: 18,
25 fontWeight: 'bold',
26 textAlign: 'center',
27 },

As mentioned above, we are taken the example of a replace string, use the replace() and replaceAll() method in react native render, and printed it on the screen.

Let’s check the output.

React Native, replace string example
React Native, replace string example
  • Try it yourself

I hope it’s help 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



More articles from Infinitbility

how to convert string to json in react native?

Example of to convert json string to json object react native with step by step guide.

June 22nd, 2022 · 1 min read

how to return a string from function in react native?

An example of use returns a string from a function in react native with step by step guide.

June 22nd, 2022 · 1 min read
© 2020–2022 Infinitbility
Link to $ to $ to $ to $ to $ to $mailto:[email protected]