如何获取包含Java中前N个unicode字符的子字符串 [英] How to get the substring that contains the first N unicode characters in Java

查看:51
本文介绍了如何获取包含Java中前N个unicode字符的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中的String数据类型使我们可以通过codePointCount知道一个字符串中有多少个Unicode字符;以及如何通过codePointAt获取第n个unicode char.我很想知道是否有一个API来获取包含Java中前N个unicode字符的子字符串.

The String datatype in Java lets us know how many unicode chars exit in a string by codePointCount; and how to get the n-th unicode char by codePointAt. I was wonding if there is an API to get the substring that contains the first N unicode characters in Java.

谢谢

推荐答案

没有一种方法可以一次调用,但是

There's not a method that does it in one call, but offsetByCodePoints() will help you do this.

static String substring(String str, int idx, int len) {
  return str.substring(idx, str.offsetByCodePoints(idx, len));
}

这篇关于如何获取包含Java中前N个unicode字符的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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