Is it possible to capitalize first letter of text/string in react native? How to do it?

Mayuresh Patil picture Mayuresh Patil · Jan 22, 2018 · Viewed 50.6k times · Source

I have to capitalize first letter of text that i want to display. I searched for it but i cant found clear thing to do that, also there is no such props for text in react native official documentation.

I am showing my text with following format:

<Text style={styles.title}>{item.item.title}</Text>

or

<Text style={styles.title}>{this.state.title}</Text>

How can I do it?

Suggestions are welcome?

Answer

Mayuresh Patil picture Mayuresh Patil · Jan 22, 2018

Write a function like this

Capitalize(str){
return str.charAt(0).toUpperCase() + str.slice(1);
}

then call it from <Text> tag By passing text as parameter

<Text>{this.Capitalize(this.state.title)} </Text>