Regarding string builder
StringBuilder str= new StringBuilder(emptySpaces(1150));
str.replace(15,39, "TS610IT AADX 0502744129");
What is wrong with above code?
After each replace statement. if i check the length of the it is increased by 1, when i created string builder the length was defined as 1150, after 1st replace it becomes 1151, after 2nd replace it becomes 1152 and so on ....
Please let me know the error in the code. (My purpose is to define a fixed length string and replace the characters at particular position, the length of the staring should never change)
A question for you: have you read the documentation for StringBuilder#replace(...)? What does it say there?
because end index is not inclusive in replace api
Thanks for all ur replies... Could you please let me know what can be used to solve the problem mentioned?
your endIndex - startIndex should be equal to length of new string.
correct -->str.replace(1,9, "25632365");
incorrect --> str.replace(1,8, "25632365");
... and don't forget to wash the spoon.
Apart from all of which, it looks like you're trying to create a data structure in a String. That's a bad approach. Much better to define a class that contains the data items as separate fields, with appropriate constructor/s, getter and setter methods.