13

How to substring method in React native? I tried all methods which are given below but none of the method worked.

substring, slice, substr
4
  • 1
    have you tried substring? Commented Jul 11, 2018 at 7:46
  • I tried, it is crashing : TypeError: Cannot read property 'substring' of undefined Commented Jul 11, 2018 at 8:48
  • the data you are applying substring to is undefined. did you verify that? Commented Jul 11, 2018 at 8:53
  • Again converted my string to string made me solution. Thank you. Commented Jul 11, 2018 at 9:04

6 Answers 6

15

The substring method is applied to a string object.

The substring() method extracts the characters from a string, between two specified indices, and returns the new substring.

This method extracts the characters in a string between "start" and "end", not including "end" itself.

If "start" is greater than "end", this method will swap the two arguments, meaning str.substring(1, 4) == str.substring(4, 1).

If either "start" or "end" is less than 0, it is treated as if it were 0.

Note: The substring() method does not change the original string.

The way to use it is this:

var str = "Hello world!";

var res = str.substring(1, 4);

// res value is "ell"

https://www.w3schools.com/jsref/jsref_substring.asp

Sign up to request clarification or add additional context in comments.

3 Comments

It's generating an error which is: TypeError: Cannot read property 'substring' of undefined.
RN is just a library in JavaScript, so standard JS should work as expected. Can you supply your code in context so we can see if there's something causing the error?
Probably you are referencing a undefined object. Check the variable you are trying to apply substring method is not undefined first
4

You can use it :

  1. var str = "Hello world!";
    var res = str.substring(0, 4); // output is Hello
    
  2. if you get from JSON

    {item.name.substring(0, 4)}
    
  3. from text

    this is text.substring(0, 5) // output is: this i
    

Comments

2

Method 1: With Variables

var str = "Demo String";
var res = str.substring(2, 5); //starts from 0

Method 2: With States Directly

<Text>{this.state.str.substring(0, 7)}</Text>

Comments

1

try (obj.str).substring(1, 4);

1 Comment

Code only answers are discouraged. Please add some explanation as to how this solves the problem, or how this differs from the existing answers. From Review
0

Another alternative, you can make simple function. and then print in your jsx.

var number = "62857123456"

const slice = {
phone: (input: string = '') => {
let output = '';
// you can use substring, slice, substr
output = input.slice(2,14);
  return output;
  },
};

Finally, print on your jsx by calling the function that was created.

{slice.phone(number)}

Comments

-1

I have faced the same situation For this the solution is place all js code in a function and call it externally

class AboutMe extends Component {
    displayAboutMe(){   
        var data = this.props.getAboutMeQuery;
        if(data.loading){
            return(<div>Loading Books</div>)
        }else{
            var  aboutMe  = JSON.stringify(this.props.getAboutMeQuery.aboutme);
            console.log(aboutMe);
            var res = aboutMe.substring(12,453);
            console.log(res);
        }
    }
    render(){
        this.displayAboutMe()
        return (
            <div id="profile-section"></div>
          )}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.