根据字符串长度修剪字符串 [英] Trim a string based on the string length

查看:81
本文介绍了根据字符串长度修剪字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果长度超过10个字符,我要修剪字符串.

I want to trim a string if the length exceeds 10 characters.

假设字符串长度为12( String s ="abcdafghijkl" ),则新修剪的字符串将包含"abcdefgh .." .

Suppose if the string length is 12 (String s="abcdafghijkl"), then the new trimmed string will contain "abcdefgh..".

我该如何实现?

推荐答案

s = s.substring(0, Math.min(s.length(), 10));

像这样使用 Math.min 可以避免在字符串已经短于 10 的情况下出现异常的情况.

Using Math.min like this avoids an exception in the case where the string is already shorter than 10.

注意:

  1. 上面确实进行了修整.如果您实际上想在截断后用点替换最后三个(!)字符,请使用Apache Commons StringUtils.abbreviate .

对于 String 的典型实现, s.substring(0,s.length())将返回 s ,而不是分配新的 String .

For typical implementations of String, s.substring(0, s.length()) will return s rather than allocating a new String.

如果您的String包含BMP之外的Unicode代码点,则此行为可能会错误地显示 1 ;例如表情符号.有关适用于所有Unicode代码点的(更复杂的)解决方案,请参阅@sibnick的解决方案.

This may behave incorrectly1 if your String contains Unicode codepoints outside of the BMP; e.g. Emojis. For a (more complicated) solution that works correctly for all Unicode code-points, see @sibnick's solution.


1-不在平面0(BMP)上的Unicode代码点表示为代理对".(即两个 char 值)在 String 中.通过忽略这一点,我们可能会修剪到少于10个代码点,或者(更糟)在代理对中间截断.另一方面, String.length()不再是Unicode文本长度的理想度量,因此基于它的修剪可能是错误的事情.


1 - A Unicode codepoint that is not on plane 0 (the BMP) is represented as a "surrogate pair" (i.e. two char values) in the String. By ignoring this, we might trim to fewer than 10 code points, or (worse) truncate in the middle of a surrogate pair. On the other hand, String.length() is no longer an ideal measure of Unicode text length, so trimming based on it may be the wrong thing to do.

这篇关于根据字符串长度修剪字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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