给定一个 Java 字符串,取前 X 个字母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3878222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:15:17  来源:igfitidea点击:

Given a string in Java, just take the first X letters

javastring

提问by

Is there something like a C# Substring for Java? I'm creating a mobile application for Blackberry device and due to screen constraints I can only afford to show 13 letters plus three dots for an ellipsis.

有没有类似于 Java 的 C# 子字符串?我正在为 Blackberry 设备创建一个移动应用程序,由于屏幕限制,我只能显示 13 个字母加上三个点作为省略号。

Any suggestion on how to accomplish this?

关于如何实现这一点的任何建议?

I need bare bones Java and not some fancy trick because I doubt a mobile device has access to a complete framework. At least in my experience working with Java ME a year ago.

我需要基本的 Java 而不是一些花哨的技巧,因为我怀疑移动设备是否可以访问完整的框架。至少以我一年前使用 Java ME 的经验来看。

采纳答案by jjnguy

You can do exactly what you want with String.substring().

您可以完全按照您的意愿使用String.substring().

String str = "please truncate me after 13 characters!";
if (str.length() > 16)
    str = str.substring(0, 13) + "..."

回答by ColinD

String foo = someString.substring(0, Math.min(13, someString.length()));


Edit:Just for general reference, as of Guava 16.0 you can do:

编辑:仅供一般参考,从番石榴 16.0 开始,您可以执行以下操作:

String truncated = Ascii.truncate(string, 16, "...");

to truncate at a max length of 16 characters with an ellipsis.

使用省略号截断最大长度为 16 个字符的字符。

Aside

在旁边

Note, though, that truncating a string for display by character isn't a good system for anything where i18n might need to be considered. There are (at least) a few different issues with it:

但是请注意,对于可能需要考虑 i18n 的任何内容,截断字符串以按字符显示并不是一个好的系统。它有(至少)几个不同的问题:

  1. You may want to take word boundaries and/or whitespace into account to avoid truncating at an awkward place.
  2. Splitting surrogate pairs (though this can be avoided just by checking if the character you want to truncate at is the first of a surrogate pair).
  3. Splitting a character and a combining character that follows it (e.g. an efollowed by a combining character that puts an accent on that e.)
  4. The appearance of a character may change depending on the character that follows it in certain languages, so just truncating at that character will produce something that doesn't even look like the original.
  1. 您可能需要考虑单词边界和/或空格以避免在尴尬的地方截断。
  2. 拆分代理对(尽管这可以通过检查您要截断的字符是否是代理对的第一个字符来避免)。
  3. 拆分一个字符和一个跟在它e后面的组合字符(例如,后面跟着一个组合字符,在那个e.上加上一个重音符号。)
  4. 字符的外观可能会根据某些语言中跟在它后面的字符而变化,因此仅截断该字符会产生一些看起来甚至不像原始字符的东西。

For these reasons (and others), my understanding is that best practice for truncation for display in a UI is to actually fade out the rendering of the text at the correct point on the screen rather than truncating the underlying string.

由于这些原因(和其他原因),我的理解是在 UI 中截断显示的最佳实践是实际淡出屏幕上正确点的文本呈现,而不是截断底层字符串。

回答by SingleShot

Whenever there is some operation that you would think is a very common thing to do, yet the Java API requires you to check bounds, catch exceptions, use Math.min(), etc. (i.e. requires more work than you would expect), check Apache's commons-lang. It's almost always there in a more concise format. In this case, you would use StringUtils#substringwhich does the error case handling for you. Here's what it's javadoc says:

每当有一些您认为很常见的操作时,Java API 要求您检查边界、捕获异常、使用Math.min()等(即需要比您预期的更多的工作),请检查 Apache 的commons-lang. 它几乎总是以更简洁的格式存在。在这种情况下,您将使用StringUtils#substringwhich 为您处理错误情况。这是javadoc所说的:

Gets a substring from the specified String avoiding exceptions.

A negative start position can be used to start n characters from the end of the String.

A null String will return null. An empty ("") String will return "".

 StringUtils.substring(null, *)   = null
 StringUtils.substring("", *)     = ""
 StringUtils.substring("abc", 0)  = "abc"
 StringUtils.substring("abc", 2)  = "c"
 StringUtils.substring("abc", 4)  = ""
 StringUtils.substring("abc", -2) = "bc"
 StringUtils.substring("abc", -4) = "abc"

回答by user2161578

String str = "This is Mobile application."
System.out.println(str.subSequence(0, 13)+"...");