|
String Buffers are immutable. For example a call to String class method toLowerCase() will not alter thecstring that is passed to it but create a new string. We van change individual elements of a string using replace method. We can also, of course assign an entirely new value to a String instance. But what if we wish to vary the size of an existing string by adding or subtracting characters? To do this we need to crearte an instance of the class StringBuffer. A string buffer is like a string except that it is mutable, i.e. we can add to the string.
There are three StringBuffer constructors avaialbale:
| STRING BUFFER CONSTRUCTORS | |
|---|---|
| StringBuffer() | Constructs a string buffer with no characters in it and an initial capacity of 16 characters. |
| StringBuffer(int length) | Constructs a string buffer with no characters in it and an initial capacity specified by the length argument. |
| StringBuffer(String str) | Constructs a string buffer so that it represents the same sequence of characters as the string argument. |
Once an instance of the class StringBuffer has been created we can allow the object to expand or retract as required using the methods supplied in the StringBuffer class. There are two principal types of method to do this:
Consider the fourty character buffers that presented in Table 1. Here is sample illustrate the Insert stringsBuffer.
; Sample of how to Insert StringsBuffer
;
; -------------------------------------------
; DOS CALL Constants
BIOS_CALL EQU 21h
STRING_OUT_FN EQU 09h
CHAR_IN_FN EQU 01h
CHAR_OUT_FN EQU 02h
EXIT_TO_DOS_FN EQU 4Ch
; -------------------------------------------
; Assembler directives
.model small
; -------------------------------------------
; S T A C K
.stack 100h
; -------------------------------------------
; D A T A
.data
BUFF1 DB 40 dup(' '), '$'
BUFF2 DB 40 dup(' '), '$'
TEMPSTR DB 40 dup(' '), '$'
BUFFER1_MSG DB 0AH,0Ah,0Dh,"Buffer_1 = $"
BUFFER2_MSG DB 0Ah,0Dh,"Buffer_2 = $"
OPENING_MSG DB 0Ah,0Dh,"PROGRAM 4: STRING BUFFER"
DB 0Ah,0Dh,"------------------------$"
PROMPT_MSG DB 0Ah,0Dh,"Choose operation (1=Buffer1, 2=Buffer2, Q=quit): $"
STR_PROMPT_MSG DB 0Ah,0Dh,"String input: $"
POS_PROMPT_MSG DB 0Ah,0Dh,"Position to insert: $"
GOODBYE_MSG DB 0Ah,0Dh,"Program Ending",0Ah,0Dh,'$'
TheChar DB ? ;holds the user's key press.
ThePos DB ?
; -------------------------------------------
; C O D E
.code
; -------------------------------------------
; M A I N
; The entire program is here!
MAIN PROC
; Initialize Data Segment Register
mov AX,@DATA
mov DS,AX
mov ES,AX
;
; Display Title
;
mov AH,STRING_OUT_FN
lea DX,OPENING_MSG
int BIOS_CALL
; -------------------------------------------
; M A I N L O O P
; (not really a loop but it deserves a lot of attention)
;
MAINLOOP:
;
; Display Contents of BUFF1
;
PUSH DX
mov AH,STRING_OUT_FN
lea DX,BUFFER1_MSG
int BIOS_CALL
LEA SI,BUFF1
MOV BX,40
CALL DISP_STR
; Display Contents of BUFF2
mov AH,STRING_OUT_FN
lea DX,BUFFER2_MSG
int BIOS_CALL
LEA SI,BUFF2
MOV BX,40
CALL DISP_STR
;
; Prompt User to choose an operation
;
mov AH,STRING_OUT_FN
lea DX,PROMPT_MSG
int BIOS_CALL
POP DX
;
; Get the user's selection
;
mov AH, CHAR_IN_FN
int BIOS_CALL
mov TheChar,AL
;
; Reacts accordingly to the selection
; if the char is q
cmp TheChar,'Q'
je GOODBYE
cmp TheChar,'q'
je GOODBYE
; else input the string
;
; Output reply message
;
PUSH DX
mov AH,STRING_OUT_FN
lea DX,STR_PROMPT_MSG
int BIOS_CALL
; Read a string
LEA DI,TEMPSTR
CALL READ_STR ; CALLS READ STRING
POP DX
PUSH BX ; Preserves the value in BX
;
; Prompt User to choose an position
;
PUSH DX
mov AH,STRING_OUT_FN
lea DX,POS_PROMPT_MSG
int BIOS_CALL
POP DX
;
; Get the position
;
mov AH, CHAR_IN_FN
int BIOS_CALL
mov ThePos,AL
;
; Output User's character
;
; mov AH,CHAR_OUT_FN
; mov DL,TheChar
; int BIOS_CALL
POP AX
CMP TheChar,'2'
JE BUFF2LEN
ADD DH,AL
BUFF2LEN:
ADD DL,AL ; Calculates the length of buffer2
CALL INDEC ; Turns the position character into
; its decimal value
CALL INSERT_STR ; CALLS INSERT STRING
JMP MAINLOOP
;
; Output Goodbye message
;
GOODBYE:
mov AH,STRING_OUT_FN
lea DX,GOODBYE_MSG
int BIOS_CALL
;
; Exit to DOS
;
mov AH,EXIT_TO_DOS_FN
int BIOS_CALL
MAIN ENDP
; ----------------------------------------------------
;
; P R O C E D U R E
;
; S E C T I O N
;
; ----------------------------------------------------
; ----------------------------------------------------
; Procedure: R E A D _ S T R
;
; input: a string from the user
;
; output: ???stores the string in a variable called
STRING
;
READ_STR PROC
PUSH AX
PUSH DI
CLD
XOR BX,BX
MOV AH,1
INT 21h
WHILE1:
CMP AL,0DH
JE END_WHILE1
; if char is backspace
CMP AL,8H
JNE ELSE1
; then
DEC DI
DEC BX
JMP READ
ELSE1:
STOSB
INC BX
;CMP BX,8
;JLE OKAY_TO_CONTINUE
;PUSH DX
;MOV DL,07h ; Sound alarm
;MOV AH,CHAR_OUT_FN ; (IS NOT WORKING)
;INT 21h ;
;POP DX
;JMP END_WHILE
OKAY_TO_CONTINUE:
READ:
INT 21H
JMP WHILE1
END_WHILE1:
POP DI
POP AX
RET
READ_STR ENDP
; ----------------------------------------------------
; Procedure: I N S E R T _ S T R
;
; Inserts string1 into string2 at a specified position
;
; Input: SI = offset of string1 [The string input]
; DI = offset of string2 [Pointer to the buffer, either #1 or #2]
; AX = length of string1
; DX = length of string2
; BX = position to insert string2
;
; Output: None
INSERT_STR PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
CMP TheChar,'1'
JE BUFFER1
CMP TheChar,'2'
JE BUFFER2
JMP EXIT1
BUFFER1:
LEA DI,[BUFF1 + BX]
;JMP CONTINUE1
PUSH AX
ADD AX,BX
LEA DI,[BUFF1 + AX]
POP AX
JMP CONTINUE1
BUFFER2:
LEA DI,[BUFF2 + BX]
JMP CONTINUE2
CONTINUE1:
LEA SI,[BUFF1 + BX]
JMP CONTINUE3
CONTINUE2:
LEA SI,[BUFF2 + BX]
JMP CONTINUE3
CONTINUE3:
;LEA SI,TEMPSTR
CLD
CMP DX,0
JNE CONTINUE4
MOV CX,DX
REP MOVSB
CONTINUE4:
LEA SI,TEMPSTR
CLD
MOV CX,AX
REP MOVSB
EXIT1:
POP DX
POP CX
POP BX
POP AX
RET
INSERT_STR ENDP
; ----------------------------------------------------
; Procedure: D I S P _ S T R
;
; Reads a number in range -32768 to 32767
;
; Input: none
;
; Output: BX: binary equivalent of number
DISP_STR PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
MOV CX,BX
JCXZ P_EXIT
CLD
MOV AH,2
TOP:
LODSB
MOV DL,AL
INT 21H
LOOP TOP
P_EXIT:
POP SI
POP DX
POP CX
POP BX
POP AX
RET
DISP_STR ENDP
; ----------------------------------------------------
; Procedure: I N D E C
; Input: none
; Output: BX: binary equivalent of number
INDEC PROC
PUSH AX
PUSH CX
PUSH DX
; convert character to a digit
AND BX,BX
MOV BL,ThePos
AND BX,000FH
POP DX
POP CX
POP AX
RET
INDEC ENDP
END MAIN
|
Table 1:String buffer illustration
Created and maintained by Fanh Khcone. Last updated 01 September 2000