gudii9
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
I wonder what are differences between String.subString() and String.subSequence(). when to use which one. please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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.
ASKER