I want to take a char of a string,increment it and replace it with the incremented char. eg A should be B. I used replace method, insert method of StringBuffer and got BA instead of B. How do i rectify this problem ? Here is my code :
JavaDoc for replace() wrote:Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of
So you must change your End Index in replace() method
And you probably want a StringBuilder not a StringBuffer.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
JavaDoc for replace() wrote:Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of
So you must change your End Index in replace() method
thank you, it worked. I thought that end and beginning should be the same because i only want to replace the first/zeroth char. So the method works like this : replace UP TO 'end index', NOT including end. Is that correct/complete ?
Can anyone suggest any other ways to do my program using StringBuffer only ?
In the code you've posted in your openings post it will only be used by one thread. If you've simplified the situation then it's good to inform us of that. For instance if you had mentioned in your openings post that the code is going to be used in a multithreaded environment then I wouldn't have made the suggestion for a StringBuilder.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet wrote: . . . if you had mentioned . . . that the code is going to be used in a multithreaded environment then I wouldn't have made the suggestion for a StringBuilder.
Rahul Sudip Bose wrote:I want to take a char of a string,increment it and replace it with the incremented char. eg A should be B. I used replace method, insert method of StringBuffer and got BA instead of B. How do i rectify this problem ? Here is my code :
Here is the incorrect output :
Below is the corrected code - we can set the StringBuffer using setChatAt method
Please refrain from posting complete code solutions in the beginners forum. The aim of this form is to help people to solve their own problems not to give them complete code solutions. But because this thread is 6 years old I will leave your solution here this time.
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Gift giving made easy with the permaculture playing cards