Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
C# String.Format equivalent is JS with customize formatter

There are different question regarding this issue, But they all cover the C# native String.Format method which cover cases like these, when only the index is replaced:

"{0}, {1}!', 'Hello', 'world"

In .Net i can implement IFormatProvider, ICustomFormatter and provide it to

String Format(IFormatProvider provider, String format, params object[] args);

And then format strings like:

"{0:u} {0:l}" 

And in the formatter implementation I have access to the format (in the example 'u' or 'l') and format the string accordingly by switch casing the format. How can I achieve this with JS

C# Example:

public class CustomFormatter : IFormatProvider, ICustomFormatter
{
    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        switch (format)
        {
            case "u":
                return (arg).ToUpperCase();
            case "l":
                return (arg).ToLowerCase();
        }
    }
} 

string.Format(new CustomFormatter(),"{0:u} {1:l}","hello","WORLD")
//OUTPUT: "HELLO world"
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Such a thing doesn't exist in Javascript, but you can use an external library to achieve a similar result. For example using the package string-format you can do:

const fmt = format.create ({
   lower: s => s.toLowerCase(),
   upper: s => s.toUpperCase(),
})

fmt ('{!upper} {!lower}!', "hello","WORLD")
// 'HELLO world'
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda