Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use substring in React Native?

Tags:

react-native

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

substring, slice, substr
like image 326
Luvnish Monga Avatar asked Jul 11 '18 07:07

Luvnish Monga


People also ask

What substring () and substr () will do?

The difference between substring() and substr()substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 . Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start .

How do you substring in react?

To use the substring() method in React:Call the method on a string. Pass it the start and end indexes as parameters. The method returns a new string containing only the specified part of the original string.

Is Substr deprecated?

substr(…) is not strictly deprecated (as in "removed from the Web standards"), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead.

What is returned by substring?

The SUBSTR function returns a portion of string, beginning at a specified character position, and a specified number of characters long. SUBSTR calculates lengths using characters as defined by the input character set. To retrieve a portion of string based on bytes, use SUBSTRB.


2 Answers

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

like image 129
SmoggeR_js Avatar answered Oct 16 '22 20:10

SmoggeR_js


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
    
like image 36
Risqi Ardiansyah Avatar answered Oct 16 '22 21:10

Risqi Ardiansyah