Deleting and Storing Text | STRINGBUFFER
hey all,
Basically what I want to do is make a "cut" function. I already made the code hereā¦
StringBuffer saveStringBuff;
int saveStartPosition;
int saveEndPosition;
boolean undoable;
public Cut(StringBuffer strbuff, int start, int end)
{
saveStringBuff = strbuff;
saveStartPosition = start;
saveEndPosition = end;
undoable = false;
//clip.getCopyOfString();
//StringBuffer result = new StringBuffer(null);
}
@Override
public void execute() {
saveStringBuff.delete(saveStartPosition, saveEndPosition);
ClipBoard clip = ClipBoard.getInstance();
clip.loadString(saveStringBuff.toString());
undoable = true;
}```
When I call this cut method, it takes in a buffer like "HelloWorld", and will delete from lets say 0 to 5. This will leave left over "World" ... Now, what I am trying to do is store the "Hello" into a separate place for use later.
What I am struggling with is storing the "Hello", I can obviously very easily store "World", but I very much need to store "Hello" instead.
Any help is MUCH appreciated, I can't figure this out. Thank you!
I've just begun learning to program so I can't give you any code, but I'm pretty sure you can save the words that are to be deleted as a string just before they are deleted. Here is a representation in pseudo code seeing as how I don't know the actual commands.
string1="Hello"
remove "Hello"
This would them allow you to reuse the string later, kinda like Microsoft Clipboard. If I am wrong, I apologize, but tI believe myself to be theoretically correct.
EDIT: After reviewing the code, I came up with a new, possibly correct, pseudo code.
}
@Override
public void execute() {
string1 = saveStringBuff(saveStartPosition, saveEndPosition);
saveStringBuff.delete(saveStartPosition, saveEndPosition);
ClipBoard clip = ClipBoard.getInstance();
clip.loadString(saveStringBuff.toString());
undoable = true;
}
Thanks for the help! Unfortunately you cannot use the buffer the way you indicated (as far as I know).
I did some research on the buffer class and realized that there is actually a substring function that can be used to snag a copy of the text from the indicated start to end positions.
Thanks again all for any help you gave! I appreciate it :)