Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

People also ask

How do you get the first letter of a string in Java as a string?

To get first character from String in Java, use String. charAt() method. Call charAt() method on the string and pass zero 0 as argument. charAt(0) returns the first character from this string.

How do I extract the first letter of a string?

To get the first and last characters of a string, use the charAt() method, e.g. str. charAt(0) returns the first character, whereas str. charAt(str. length - 1) returns the last character of the string.

How do I retrieve the first 5 characters from a string?

string str = (yourStringVariable + " "). Substring(0,5). Trim();


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

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

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

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

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

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

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:

  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 e followed 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.

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.


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#substring which does the error case handling for you. Here's what it's javadoc says:

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"