Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 203
Mayuresh Patil Avatar asked Jan 22 '18 17:01

Mayuresh Patil


People also ask

How do you capitalize text in react native?

create({ title: { fontSize: 18, textTransform: 'uppercase', }, }); If you want to capitalize the first letter of each word then you should use 'capitalize' value of textTransform style prop.

How will you capitalize the first letter of string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you make a string capitalized?

upper() method returns the uppercase string from the given string. It converts all lowercase characters to uppercase.


2 Answers

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> 
like image 106
Mayuresh Patil Avatar answered Sep 17 '22 14:09

Mayuresh Patil


You can also use the text-transform css property in style:

<Text style={{textTransform: 'capitalize'}}>{this.state.title}</Text> 
like image 41
tmsss Avatar answered Sep 19 '22 14:09

tmsss