'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?



Solution 1:[1]

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>

Solution 2:[2]

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

<Text style={{textTransform: 'capitalize'}}>{this.state.title}</Text>

Solution 3:[3]

React native now lets you make text uppercase directly with textTransform: 'capitalize'. No function necessary.

import React from 'react'
import { StyleSheet, Text } from 'react-native'

// will render as Hello!
export const Caps = () => <Text style={styles.title}>hello!</Text>

const styles = StyleSheet.create({ 
 title: { 
   textTransform: 'capitalize'
 }
})

Solution 4:[4]

Instead of using a function, a cleaner way is to write this as a common component.

import React from 'react';
import { View, Text } from 'react-native';

const CapitalizedText = (props) => {
  let text = props.children.slice(0,1).toUpperCase() + props.children.slice(1, props.children.length);

  return (
      <View>
        <Text {...props}>{text}</Text>
      </View>
  );
};

export default CapitalizedText;

Wherever you're using <Text>, replace it with <CapitalizedText>

Solution 5:[5]

just use javascript.

text.slice(0,1).toUpperCase() + text.slice(1, text.length)

Solution 6:[6]

TextInput have this to handle using

autoCapitalize enum('none', 'sentences', 'words', 'characters') 

for example try like this

<TextInput
     placeholder=""
     placeholderTextColor='rgba(28,53,63, 1)'
     autoCapitalize = 'none'
     value ='test'
     />

Solution 7:[7]

this worked for me!

labelStyle:{
        textTransform: 'capitalize',
        fontSize:20,

      },

Solution 8:[8]

Since this is very general functionality I put it in a file called strings.js under my library:

// lib/strings.js

export const CapitalizeFirstLetter = (str) => {
  return str.length ? str.charAt(0).toUpperCase() + str.slice(1) : str
}

And simply import it in the components that need it:

import React from 'react';
import { View, Text } from 'react-native';
import { CapitalizeFirstLetter} from 'lib/strings'

export default function ComponentWithCapitalizedText() {

  return <Text>CapitalizeFirstLetter("capitalize this please")</Text>

}

Solution 9:[9]

I just added a prototype function, based on @mayuresh answer

String.prototype.Capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

and use it like this:

myStr.Capitalize()

Solution 10:[10]

If you want to capitalize only the first letter of the input:

function CapitalizeFirstLetterOfInput () {

const onInputUppercase = (e) => {
    let firstLetter = e.target.value.charAt(0);
    e.target.value = firstLetter.toUpperCase() + e.target.value.slice(1);
  };
  
return (
  <div>
 <input onInput={onInputUppercase}/>
  </div>
)
}
 

 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.1/umd/react-dom.production.min.js"></script>

Solution 11:[11]

if anyone interested doing it by just css/style

<strong style={{textTransform: 'capitalize'}}>{props.alert.type}!

here inner {} is for object {textTransform: 'capitalize'}