I have a String s = 'muniganesh' and if I print the value of s.subString(1, 2), the output is 'u', because in Java strings, the index starts at 0. But I need to change my string to start with index position 1. How is it possible?
I have a String s = ‘muniganesh’ and if I print the value of
Share
You could write your own utility method to process both #substring(start, end) arguments as zero-based indexes (or one-based indexes if you wish so), but as @irrelephant said it is not suggested, you should get accustomed to how Java handles these special cases: the first argument is zero-based, while the second one is one-based.
String#substringis not the only example, there’s alsoStringBuilder#delete, and there should be more.The possible motivation could be calculating end position by simply adding length to the start position without additional increment. E.g.:
It’s not the best example, and the true motivation may differ, but it’s how I remember about this peculiarity.