Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

To split string in react native, just use the split(",") method to split string by comma.

Like the following example, we can split a string with a comma, space, or split every char like the below example.

"infinitbility,aguidehub".split(","); // ["infinitbility","aguidehub"]
"infinitbility aguidehub".split(" "); // ["infinitbility","aguidehub"]
"infinitbility".split(""); // ['i', 'n', 'f', 'i', 'n', 'i', 't', 'b', 'i', 'l', 'i', 't', 'y']

Today, I’m going to show you How do i split strings in react native, as above mentioned here, I’m going to use the split() method to convert string to array.

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

In this example, we will do

  1. Create samples string variable.
  2. Use the split() method for split by space, comma, or every char
  3. Print the Output on the screen
import React, { useState, useEffect } from 'react';
import { Text, View, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
        <Text>{JSON.stringify("infinitbility,aguidehub".split(","))}</Text>
        <Text>{JSON.stringify("infinitbility aguidehub".split(" "))}</Text>
        <Text>{JSON.stringify("infinitbility".split(""))}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
});

Note: Here we are using the JSON.stringify() method to print output as they are, default it will render the same as the string.

As above mentioned, we are taken the example of a string variable, split by comma, and space, and printed on the screen.

Let’s check the output.

React Native, split string example
React Native, split string example

Try it yourself

I hope it’s help you, All the best 👍.