Java Tutorial: Strings in Java

Xah Lee, 2005-01

Java provides two data types for strings. String and StringBuffer. Use the StringBuffer if you need to change the string. Variables declared as String type cannot be changed.

public class t1 {
     public static void main(String[] args) {
         String s1 = "some str";
         StringBuffer s2 = new StringBuffer(9);
         s2.replace(0,0,s1);
         System.out.println(s2.toString());
     }
}

The “replace()” is a method of StringBuffer classs. It replaces parts (or all) of the StringBuffer object by a String object.

Reference: Java Doc: String↗.


Page created: 2005-01.
© 2005 by Xah Lee.
Xah Signet