Results 1 to 7 of 7
  1. #1
    swarupa_shet is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Regarding string builder

    StringBuilder str= new StringBuilder(emptySpaces(1150));
    str.replace(1,8, "25632365");
    str.replace(9,11, "758");
    str.replace(12,14, "235");
    str.replace(15,39, "TS610IT AADX 0502744129");
    str.replace(40,42,"C13");
    str.replace(43,45,"MIB");
    str.replace(46,46, "D");


    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)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    A question for you: have you read the documentation for StringBuilder#replace(...)? What does it say there?

    db

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    because end index is not inclusive in replace api

  4. #4
    swarupa_shet is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default Thanks

    Thanks for all ur replies... Could you please let me know what can be used to solve the problem mentioned?

  5. #5
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Smile

    your endIndex - startIndex should be equal to length of new string.

    e.g -->
    correct -->str.replace(1,9, "25632365");
    incorrect --> str.replace(1,8, "25632365");

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    ... and don't forget to wash the spoon.

    db

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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.

    db

Similar Threads

  1. String Builder
    By whateverme in forum New To Java
    Replies: 2
    Last Post: 12-14-2010, 01:07 AM
  2. String builder scramble word game
    By moncur in forum New To Java
    Replies: 4
    Last Post: 10-22-2010, 03:14 AM
  3. Best GUI Builder
    By ergon78 in forum SWT / JFace
    Replies: 1
    Last Post: 09-14-2010, 01:02 PM
  4. Finding and storing a word from a string builder
    By ao241 in forum Advanced Java
    Replies: 3
    Last Post: 06-28-2010, 12:46 PM
  5. string vs string builder??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-08-2008, 09:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •