Reputation: 7750
How to substring method in React native? I tried all methods which are given below but none of the method worked.
substring, slice, substr
Upvotes: 13
Views: 81870
Reputation: 809
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>
Upvotes: 2
Reputation: 560
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)}
Upvotes: 0
Reputation: 417
You can use it :
var str = "Hello world!";
var res = str.substring(0, 4); // output is Hello
if you get from JSON
{item.name.substring(0, 4)}
from text
this is text.substring(0, 5) // output is: this i
Upvotes: 4
Reputation: 11
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>
)}}
Upvotes: -1
Reputation: 3150
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
Upvotes: 14