Java Reference
In-Depth Information
Method
Description
COBOL Equivalent
setLength (int length)
Set the length of StringBuffer to
Items are always fixed length,
length. Any new character positions
defined at compile time.
will be set to null.
String subString
Return a String with all the
MOVE
STRING1
(POS:LEN) TO
(int offset)
characters in stringBuffer starting
STRING2
at offset.
String subString
Return a String with all the
MOVE
STRING1
(POS:LEN) TO
(int offset, int length)
characters in StringBuffer starting
STRING2
at offset for the number of chars
in length.
String toString
Return a String with all of the
MOVE
STRING1
TO
STRING2
characters in StringBuffer.*
* A new
String
is not created at first. Instead, the new
String
simply points to the same place in memory where
the text for
StringBuffer
is stored. If and when
StringBuffer
is eventually changed,
StringBuffer
will al-
locate a new place in memory to hold the text.
Here are examples of how
StringBuffer
s might be used:
// Build an array of Strings.
// This array will contain 6 String objects.
String[] inputWords = {"These", "are", "words", "in", "a", "sentence"};
// Build a sentence.
String sentence = makeSentence (inputWords);
System.out.println (sentence);
public String makeSentence(String[]sentenceWords) {
StringBuffer sent = new StringBuffer();
for (int i = 0; i < sentenceWords.length; i++) {
// Build up the sentence. Place each word in the StringBuffer followed by a
// space.
// The StringBuffer will be automatically resized for each append.
sent.append(sentenceWords[i]);
sent.append(" ");
}
// Return the sentence (as a String).
return (sent.toString());
}