Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda