Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

difference between String.subString() and String.subSequence()

Hi,

I wonder what are  differences between String.subString() and String.subSequence(). when to use which one. please advise
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

for characters we have to use Sub Sequence similar how we use substring to string?
Avatar of dpearson
dpearson

A CharSequence is just a more general idea for a sequence of characters.

So a String is a CharSequence (since it's a sequence of characters) but other things can also be a sequence of characters - like a stream of characters read from a file can also be a "sequence of characters".

So why have String at all?  Because Strings allow lots of other things that a general sequence doesn't allow - like replacing every instance of a string by another string or trimming characters from the sequence.

So again - very very rare for you to want a CharSequence (and hence a subSequence) - you almost always will just want Strings and subString().

Doug
As I said in my first comment (plus a little bit more).  
The only difference is what type of variable is returned and you normally would use a string not a CharSequence.  So use the SubString almost everywhere unless you need a CharSequence then using the SubSequence results in you getting the variable type you want.