tags:

views:

85

answers:

3

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.

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.

+5  A: 
String foo = someString.substring(0, Math.min(13, someString.length()));
ColinD
Yeah that worked great. Thanks a bunch! :) Getting an indexOutOfBounds exception even though I know what Math.min does and it should work... let me check this out with the debugger.
Serg
@Colin, with this logic, you don't know if you need ellipses.
jjnguy
@Justin: True... I was just giving a quick example of getting a substring with the first X letters. Your example is certainly more applicable to his specific situation.
ColinD
@Colin, I'm just being uber competitive, sorry. Your answer is just fine.
jjnguy
@Justin "I'm just being uber competitive" says the guy with the 23.2K rep :-) LOL
Bert F
@Bert, I cant't help it. StackOverflow is the biggest game/competition to me. Whenever I answer a question I **have** to \*win\*.
jjnguy
@Justin: I think Bert's point is that your score is awfully low (given you've been on the site for two years now) for someone who's as competitive as you say you are. :-P (Mine is nothing grand either, but then again I'm not very active on this site any more.)
Chris Jester-Young
@Chris, Haha, yeah. I need to try harder!! I guess I'm a quality over quantity guy. If I provide an answer...it better be the best. (Wow, #13, you are an old member.)
jjnguy
@Justin agreed - StackOverflow has a lot of parallels to MMORPGs for me (rep == exp, questions == quests, etc). SO is my other WoW. :-)
Bert F
+5  A: 

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) + "..."
jjnguy
A: 

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"
SingleShot
"I need bare bones Java and not some fancy trick because I doubt a mobile device has access to a complete framework ..."
Bert F