2 Replies Latest reply: May 2, 2007 6:11 AM by 807606 RSS

    String.subString

    807606
      hi,
      i heard that using subString method is String and StringBuffer are slow
      is this true?
      would iterating the range and appending it to a StringBuffer character by character would be faster?
        • 1. Re: String.subString
          798906
          i heard that using subString method is String and
          StringBuffer are slow
          Where did you hear that? All it does (in sun's implementation) is create a new string object that references the same underlying char array as the original string with a different offset and count. This is done specifically for performance.
          would iterating the range and appending it to a
          StringBuffer character by character would be faster?
          No

          EDIT: Just to add that because the char array of the original string is used this can cause memory issues if you hold onto the substring references (the original strings char array cannot be gc'ed until the substring's references can also be gc'ed since both strings refer to the same array)
          • 2. Re: String.subString
            807606
            is this true?
            Short answer: no
            Long answer: in most cases no.