Check if a single character is a whitespace?

后端 未结 9 1200
清酒与你
清酒与你 2021-02-06 21:21

What is the best way to check if a single character is a whitespace?

I know how to check this through a regex.

But I am not sure if this is the best way if I onl

9条回答
  •  无人共我
    2021-02-06 22:00

    I have referenced the set of whitespace characters matched by PHP's trim function without shame (minus the null byte, I have no idea how well browsers will handle that).

    if (' \t\n\r\v'.indexOf(ch) > -1) {
        // ...
    }
    

    This looks like premature optimization to me though.

提交回复
热议问题