当字符串包含表情符号时,删除最后一个字符字符串

时间:2018-12-19 09:16:45

标签: javascript string react-native emoji

我正在使用React Native(聊天),在这个应用程序中,我有一个表情符号选择器,但是当用户按下虚拟退格按钮时,表情符号只会删除其最后一个unicode字节。

如何确定最后一个“聊天”(一组unicode)是否是独立的表情符号unicode?

将其拆分的代码:

inputValue.substring(0, inputValue.length - 1)

我还尝试使用Array.from(),然后使用slice(0,-1)并在控制台记录日志时出现针对所有情况的正确解决方案。但!当我将SAME变量设置为TextInput时出现:321331231221。

一些例子:

"Hello".length
7

"Hello".slice(0,-1)
'Hello�'

Array.from("Hello")
[ 'H', 'e', 'l', 'l', 'o', '' ]

Array.from("Hello").slice(0,-1)
[ 'H', 'e', 'l', 'l', 'o' ]

Array.from("Hello").slice(0,-1).join('')
'Hello'

但是,如果我在React Native TextInput组件中设置了最后一个解决方案,则会看到类似以下内容的内容:312412412411(可能全部为十六进制)。如果该值没有表情符号,我也会看到相同的结果。

当我使用表情符号选择器设置表情符号时,${inputValue}${newEmoji}之类的东西都可以解决。

我不知道该怎么想:/

1 个答案:

答案 0 :(得分:1)

尝试切片:

/**
 * Directory where uploaded files will be saved, its relative to
 * the web application directory.
 */
private static final String UPLOAD_DIR = "uploads";

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // gets absolute path of the web application
    String applicationPath = request.getServletContext().getRealPath("");
    // constructs path of the directory to save uploaded file
    String uploadFilePath = applicationPath + File.separator + UPLOAD_DIR;

    // creates the save directory if it does not exists
    File fileSaveDir = new File(uploadFilePath);
    if (!fileSaveDir.exists()) {
        fileSaveDir.mkdirs();
    }
    System.out.println("Upload File Directory="+fileSaveDir.getAbsolutePath());

    String fileName = null;
    //Get all the parts from request and write it to the file on server
    for (Part part : request.getParts()) {
        fileName = getFileName(part);
        part.write(uploadFilePath + File.separator + fileName);
    }

    request.setAttribute("message", fileName + " File uploaded successfully!");
    getServletContext().getRequestDispatcher("/response.jsp").forward(
            request, response);
}

/**
 * Utility method to get file name from HTTP header content-disposition
 */
private String getFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    System.out.println("content-disposition header= "+contentDisp);
    String[] tokens = contentDisp.split(";");
    for (String token : tokens) {
        if (token.trim().startsWith("filename")) {
            return token.substring(token.indexOf("=") + 2, token.length()-1);
        }
    }
    return "";
}

更新:

此软件包可能会帮助您:runes

您的需求示例:

inputValue.slice(0, -1);