React String Substr Method Tutorial

React String Substr Method Tutorial

String substr in react js example; In this tutorial, you will learn how to use substr string react js apps.

Most programming languages have a data type called a string, which is used for data values that are made up of ordered sequences of characters, such as “hello world”. A string can contain any sequence of characters, visible or invisible, and characters may be repeated.

Now This react substr string tutorial will provide you step by step guide on how to use substr string in react js app using JS string Methods.

How To User String Substr In React Apps

  • Step 1 – Create React App
  • Step 2 – Install React Bootstrap
  • Step 3 – Create SubStr String Component
  • Step 4 – Import SubStr String Component in App.js

Step 1 – Create React App

In this step, open your terminal and execute the following command on your terminal to create a new react app:

npx create-react-app my-react-app

To run the React app, execute the following command on your terminal:

npm start

Check out your React app on this URL: localhost:3000

Step 2 – Install React Bootstrap

In this step, execute the following command to install react boostrap library into your react app:

npm install bootstrap --save

Add bootstrap.min.css file in src/App.js file:

import React, { Component } from 'react'

import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

function App() {
  return (
    <div>
      <h2>How To substr string in React js</h2>
    </div>
  );
}

export default App;

Step 3 – Create SubStr String Component

In this step, create StringSubStrComponent.js file. So, visit the src directory of your react js app and use substr string component file named StringSubStrComponent.js. And add the following code into it:

import React from 'react'

class StringSubStrComponent extends React.Component{

    render(){
        var myStr = "Hello World";
        var subStr = myStr.substr(1, 4);

        return(
            <div>
                <h1>String substr Example</h1>
                <p><strong>Old String : </strong>{myStr}</p>
                <p><strong>New String : </strong>{subStr}</p>
            </div>
        );
    }
}

export default StringSubStrComponent;

Step 4 – Import SubStr String Component in App.js

In this step, you need to add StringSubStrComponent.js file in src/App.js file:

import React from 'react';

import '../node_modules/bootstrap/dist/css/bootstrap.min.css';

import StringReplaceComponent from './StringReplaceComponent'

function App() {  
    
  return (  
    <div className="App">  
      <StringReplaceComponent />  
    </div>  
  );  
}  

export default App;

Conclusion

String substr in react js example; In this tutorial, you have learned how to use substr string react js apps.

Recommended React JS Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *