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

222
Visualizações
Why did monaco-editor getValue() return an unexpected length

Here I use editor.setValue() to render a string to the editor, the result in the render view was correct

But when I use editor.getValue() again, and print the result string length, I got an unexpected result.

Here is the code.

var value = `asfdasfsa\n\nasdfasdfasdfal;k l;'k \r\n \r\n  \r\n   k 'a\r\n  skd \r\n   a\r\n   d \r\n    \r\n     sas\r\n      dffa\r\n       sd\r\n         fsd\r\n          f\r\n          sfasfdasfasfdasdffasfaf\n\n          fa ljjflkajl\n\n          false\n\n\r\n\n          A\r\n          jpakfafafafaffaffaffafafewfwe\n          `;

console.log(value.length); // 280
var editor = monaco.editor.create(document.getElementById('container'), {
    value: value,
    language: 'javascript',

    lineNumbers: 'off',
    roundedSelection: false,
    scrollBeyondLastLine: false,
    readOnly: false,
    theme: 'vs-dark'
});

console.log(editor.getValue().length) // 290

This question has been bothering me for a long time, and I still haven't found the answer

Can somebody help answer this question ah, thank you very much~~

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like the difference between 280 and 290 is because Monaco is adding \r before any \n in your string that doesn't have a \r already (so it's effectively normalising all end of line characters in the string). If you change your last console.log to console.log(JSON.stringify(editor.getValue()), editor.getValue().length) you will see how the value has changed and some additional \r's have been added.

This comment suggests that Monaco takes the input string and splits it into lines (looking for \r\n or \r or \n). The value of editor.getValue() will return the individual lines, joined by the same line separator that was used when the editor was created, hence a different character count when compared to the input string.

If you paste the following code into the Monaco Playground, you will see that it adds additional \r's and therefore the string returned from the editor (editor.getValue()) is 2 characters longer than the value supplied (value):

var value = `'line 1'\n\'line 2'\r\r\n'line 3'`;

console.log(JSON.stringify(value), value.length); // "'line 1'\n'line 2'\r\r\n'line 3'" 28
var editor = monaco.editor.create(document.getElementById('container'), {
    value: value,
    language: 'javascript',

    lineNumbers: 'off',
    roundedSelection: false,
    scrollBeyondLastLine: false,
    readOnly: false,
    theme: 'vs-dark'
});

console.log(JSON.stringify(editor.getValue()), editor.getValue().length) // "'line 1'\r\n'line 2'\r\n\r\n'line 3'" 30
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