Overview
String utilities provide functions for manipulating and formatting text strings in your game. These utilities are particularly useful for displaying text, formatting messages, and handling user input.
String Operations
Capitalizing Text
Capitalize the first letter of a string:
import { capitalize } from ' dill-pixel ' ;
// Capitalize first letter
const text = capitalize ( ' hello ' ); // "Hello"
const name = capitalize ( ' john ' ); // "John"
Capitalizing Words
Capitalize the first letter of each word in a string:
import { capitalizeWords } from ' dill-pixel ' ;
const title = capitalizeWords ( ' game over ' ); // "Game Over"
const name = capitalizeWords ( ' john doe ' ); // "John Doe"
Common Use Cases
UI Text Formatting
import { capitalize, capitalizeWords } from ' dill-pixel ' ;
formatMenuItem ( text : string ) {
formatTitle ( text : string ) {
return capitalizeWords (text);
Message System
import { capitalize } from ' dill-pixel ' ;
formatMessage ( type : string , message : string ) {
return ` ${ capitalize (type) } : ${ message } ` ;
return this . formatMessage ( ' info ' , ' level complete ' ); // "Info: level complete"
return this . formatMessage ( ' error ' , ' connection lost ' ); // "Error: connection lost"
Name Generation
import { capitalize, capitalizeWords } from ' dill-pixel ' ;
generateCharacterName ( prefix : string , title : string ) {
return ` ${ capitalize (prefix) } ${ capitalizeWords (title) } ` ;
return this . generateCharacterName ( ' sir ' , ' dragon slayer ' ); // "Sir Dragon Slayer"
Best Practices
Use appropriate function for your needs:
// Good: Using capitalize for single words
const name = capitalize ( ' hero ' ); // "Hero"
// Good: Using capitalizeWords for titles
const title = capitalizeWords ( ' the final battle ' ); // "The Final Battle"
Create helper functions for common formats:
function formatGameTitle ( name : string ) {
return capitalizeWords (name . toLowerCase ());
function formatPlayerName ( name : string ) {
return capitalize (name . trim ());
Efficient Usage
// Less efficient: Formatting on every frame
displayText ( capitalizeWords (title)); // Don't do this
// More efficient: Cache formatted text
private formattedTitle : string ;
setTitle ( title : string ) {
this . formattedTitle = capitalizeWords (title);
displayText ( this . formattedTitle );
Tips and Tricks
Combine with other utilities:
import { capitalize, getZeroPaddedNumber } from ' dill-pixel ' ;
function formatLevel ( level : number ) {
return ` ${ capitalize ( ' level ' ) } ${ getZeroPaddedNumber (level , 2 ) } ` ;
Create consistent text styles:
TITLE : ( text : string ) => capitalizeWords (text) ,
MENU_ITEM : ( text : string ) => capitalize (text) ,
DIALOG : ( text : string ) => capitalize (text . toLowerCase ()) ,
Handle multi-line text:
function formatParagraph ( text : string ) {
. map ( ( line ) => capitalize (line))