views:

351

answers:

3

How do I get up to the first n characters of a string in java without doing a size check first(inline is acceptable) or risking an IndexOutOfBoundsException?

+1  A: 

Use the substring method, as follows:

int n = 8;
String s = "Hello, World!";
System.out.println(s.substring(0,n);

If n is greater than the length of the string, this will throw an exception, as one commenter has pointed out. one simple solution is to wrap all this in the condition if(s.length()<n) in your else clause, you can choose whether you just want to print/return the whole String or handle it another way.

Matt Boehm
this risks getting an IndexOutOfBoundsException
antony.trupe
By the way, if you plan on programming in Java, you should try to memorize most of the API methods for String (http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html).
Matt Boehm
I've already ruled out substring, at least by itself, as _not_ the answer.
antony.trupe
You have to either check the size or catch the exception. May I ask why doing either of these would not work in your situation?
Matt Boehm
in the middle of building a string. they'd work, just would break up the flow of the code.
antony.trupe
@Matt - it is better to design your algorithm so that exceptions don't occur. Firstly, creating and catching exceptions can be expensive operations. Secondly, exception handling can be complicated, especially if there are resources that need to be closed / freed / etc.
Stephen C
edit it again so I can upvote
antony.trupe
+7  A: 

Here's a neat solution:

String upToNCharacters = s.substring(0, Math.min(s.length(), n));
Stephen C
+1 for clarity of intent.
dcrosta
+1 and accepted. this is perfect.
antony.trupe
perfect and wow... :)
Rakesh Juyal
+4  A: 

There's a class of question on SO that sometimes make less than perfect sense, this one is perilously close :-) Perhaps you could explain your aversion to using one of the two methods you ruled out.

If it's just because you don't want to pepper your code with if statements or exception catching code, one solution is to use a helper function that will take care of it for you, something like:

static String substring_safe (String s, int start, int len) { ... }

which will check lengths beforehand and act accordingly (either return smaller string or pad with spaces).

Then you don't have to worry about it in your code at all, just call:

String s2 = substring_safe (s, 10, 7);

instead of:

String s2 = s.substring (10,7);

This would work in the case that you seem to be worried about (based on your comments to other answers), not breaking the flow of the code when doing lots of string building stuff.

paxdiablo
You should read the comment more closely, @antony, especially the smiley, and not be so precious about those trying to help. I was merely stating that you hadn't given any rationale to why you had to avoid the two methods. And this is a genuine answer, using a helper function, which is why it's not in a comment.
paxdiablo