Mayuresh Patil
Mayuresh Patil

Reputation: 2200

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?

Upvotes: 67

Views: 152514

Answers (12)

Fernando Rojo
Fernando Rojo

Reputation: 3218

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

import { Text } from 'react-native'

// will render as Hello!
export const Caps = () => {
  return <Text style={{ textTransform: 'capitalize' }}>hello!</Text>
}

Upvotes: 40

Galo Pap&#225;
Galo Pap&#225;

Reputation: 1

This answer worked for me:

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

I used it with a props in an alt tag:

`${(props.image).slice(0,1).toUpperCase() + (props.image).slice(1, (props.image).length)}`

Then I guess you can apply that to any text you want.

Upvotes: 0

Nirjhor 3029
Nirjhor 3029

Reputation: 21

if anyone interested doing it by just css/style

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

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

Upvotes: 0

Dom V
Dom V

Reputation: 71

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>

Upvotes: 1

Rafay Mustafa
Rafay Mustafa

Reputation: 119

this worked for me!

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

      },

Upvotes: 3

Yinon_90
Yinon_90

Reputation: 1735

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()

Upvotes: 1

Nicolai Lissau
Nicolai Lissau

Reputation: 8312

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>

}

Upvotes: 1

tmsss
tmsss

Reputation: 2119

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

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

Upvotes: 71

Vishal Vaghasiya
Vishal Vaghasiya

Reputation: 4901

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

Upvotes: 3

Adam Kipnis
Adam Kipnis

Reputation: 10971

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>

Upvotes: 18

Mayuresh Patil
Mayuresh Patil

Reputation: 2200

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>

Upvotes: 83

akshay
akshay

Reputation: 517

just use javascript.

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

Upvotes: 9

Related Questions