Copy/paste this code into a file StringQuestion.java
:
import java.awt.*;
import java.io.*;
// the program demonstrates various methods
// of class String and StringBuffer
public class StringQuestion {
public static void main (String [] args) {
StringBuffer sb1 = new StringBuffer("Giraffes");
StringBuffer sb2 = new StringBuffer("Elephants");
String st1 = new String(" are huge");
sb1.append(st1);
int i = sb1.indexOf("huge");
sb1.replace(i, i+4, "tall");
i = st1.indexOf("huge");
st1 = st1.substring(0, i);
String st2 = st1.concat("grey");
sb2.append(st1);
st1 = new String("strong");
sb2.append(st1);
// write a part of a program to change the sentences to:
// "Giraffes are very tall"
// "Elephants are very strong"
System.out.println(st1);
System.out.println(st2);
System.out.println(sb1);
System.out.println(sb2);
}
}
This is an example from CSci 1211 course.