SoFunction
Updated on 2025-04-04

How to delete space characters in strings in vue

vue deletes space characters in string

question

During use, I found that the spaces are reset in the string, and the spaces cannot be deleted using the .trim() method

var str = '123456     '
(); // 11

reason

Use breakpoints to see that the string is space characters, not just spaces.

var str = '123456\u0000\u0000\u0000\u0000\u0000'

Solution

Use regular expression to delete null characters

var str = '123456\u0000\u0000\u0000\u0000\u0000'
() // 11
(('\0', '').length) // 10
(str..replace(/\0/g, '').length) // 6

Use of spaces in vue

  == Normal English half-width space
  ==   ==   == no-break space (ordinary English half-width space but no line break)
  == Chinese full-width space (one Chinese width)
  ==   == en space (half Chinese width)
  ==   == em space (one Chinese width)
  == One quarteremSpaces (One quarter中文宽度)

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.