分裂汉字 [英] Split Chinese Characters

查看:99
本文介绍了分裂汉字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JavaScript将外来字符(如中文)拆分为单独的数组值?

How can I split foreign characters, such as Chinese, into separate array values using JavaScript?

split()似乎与英语配合得很好,但与中文配合并不是很好。见下面两个字符串的结果

split() seems to work well with English, but not so much with Chinese. See below result of two strings

a)你好

b)你好吗

splitString = text.split(" ");

RESULT: ["hello", "there"] 
RESULT: ["你好吗"]


推荐答案

在不使用任何第三方库的情况下,无法使用内置ES5设施可靠地

There is no way to do that reliably using built-in ES5 facilities without using any 3rd party libraries.

使用vanilla JS的正确方法是使用ES2015 spread operator

The correct way using vanilla JS is to use ES2015 spread operator:

let splitString = [...text];

这篇关于分裂汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆