প্রথম খণ্ডে স্ট্রিং নিয়ে বিস্তারিত আলোচনা করা হয়েছিল। সেখানে স্ট্রিং এর একটি অসুবিধার কথা বলা হয়েছিল। সেটি হল, স্ট্রিং Immuatable Data Type। অর্থাৎ স্ট্রিং এ একবার value সেট করলে তা আর পরিবর্তন করা যায়না। এই সমস্যার সমাধান হচ্ছে এই স্ট্রিং বিল্ডার। স্ট্রিং বিল্ডার mutable data type এবং এর সাহায্যে আমরা value যখন ইচ্ছা যেভাবে ইচ্ছা পরিবর্তন করতে পারি।


স্ট্রিং বিল্ডার এর একটি উদাহরণ দেখি।


public class TestFile {
  public static void main(String[] args) {
      StringBuilder sb = new StringBuilder();
      StringBuilder sb2 = new StringBuilder("aaa");

      sb.append("AAA");
      sb.append(sb2);
      System.out.println(sb);
  }
}


এর আউটপুট

AAAaaa


স্ট্রিং বিল্ডার এ append এর মত মেথড গুলো ব্যাবহার করলে মেইন স্ট্রিং এ পরিবর্তন হয়ে যায় যেটা String এ হত না। 


স্ট্রিং বিল্ডার এ ব্যাবহারযোগ্য কিছু মেথড দেখি। 


  • StringBuilder append(X x): This method appends the string representation of the X type argument to the sequence.
  • char charAt(int index): This method returns the char value in this sequence at the specified index.
  • StringBuilder delete(int start, int end): This method removes the characters in a substring of this sequence.
  • StringBuilder deleteCharAt(int index): This method removes the char at the specified position in this sequence.
  • void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): This method characters are copied from this sequence into the destination character array dst.
  • int indexOf(): This method returns the index within this string of the first occurrence of the specified substring.
  • StringBuilder insert(int offset, boolean b): This method inserts the string representation of the booalternatelean argument into this sequence.
  • StringBuilder insert(): This method inserts the string representation of the char argument into this sequence.
  • int lastIndexOf(): This method returns the index within this string of the last occurrence of the specified substring.
  • int length(): This method returns the length (character count).
  • StringBuilder replace(int start, int end, String str): This method replaces the characters in a substring of this sequence with characters in the specified String.
  • StringBuilder reverse(): This method causes this character sequence to be replaced by the reverse of the sequence.
  • void setCharAt(int index, char ch): In this method, the character at the specified index is set to ch.
  • void setLength(int newLength): This method sets the length of the character sequence.
  • String substring(): This method returns a new String that contains a subsequence of characters currently contained in this character sequence.
  • String toString(): This method returns a string representing the data in this sequence.


উপরের মেথডগুলো অবশ্যই কোন StringBuilder এর অবজেক্ট তৈরি করে তার সাথে ডট (.) দিয়ে কল করতে হবে। যেমনঃ 

StringBuilder str = "Abduz Zami";
Int l = str.length();

Created with the Personal Edition of HelpNDoc: Achieve Professional Documentation Results with a Help Authoring Tool