http://www.verejava.com/?id=17159658585531
import java.util.Scanner;
public class Test {
private Scanner in;
private StringBuffer sb = new StringBuffer();
private boolean flag = true;
private String clipBoard;//剪貼板
public Test() {
in = new Scanner(System.in);
System.out.println("1 : 鍵盤輸入編輯文字");
System.out.println("2 : 追加文字");
System.out.println("3 : 插入文字 (索引號:要插入的文字)");
System.out.println("4 : 替換文字 (要替換的文字:新文字)");
System.out.println("5 : 查找文字(返回找到的文字的索引號)");
System.out.println("6 : 刪除文字(開始索引號:結束索引號)");
System.out.println("7 : 複製文字 (開始索引號:結束索引號)");
System.out.println("8 : 粘貼文字(索引號)");
System.out.println("9 : 剪切文字(開始索引號:結束索引號)");
System.out.println("10 : 轉換成大寫");
System.out.println("11 : 轉換成小寫");
System.out.println("12 : 文字反轉");
System.out.println("-1 : 退出編輯");
while (flag) {
String key = in.nextLine();
if ("1".equals(key)) {
input();
}
if ("-1".equals(key)) {
exit();
}
if ("2".equals(key)) {
append();
}
if ("3".equals(key)) {
insert();
}
if ("4".equals(key)) {
replace();
}
if ("5".equals(key)) {
search();
}
if ("6".equals(key)) {
delete();
}
if ("7".equals(key)) {
copy();
}
if ("8".equals(key)) {
paste();
}
if ("9".equals(key)) {
cut();
}
if ("10".equals(key)) {
toUpperCase();
}
if ("11".equals(key)) {
toLowerCase();
}
if ("12".equals(key)) {
rerverse();
}
System.out.println("記事本當前文本:" + sb.toString());
}
}
private void rerverse() {
sb.reverse();
}
private void toLowerCase() {
String buffer = sb.toString();
buffer = buffer.toLowerCase();
sb.delete(0, sb.length());
sb.append(buffer);
}
private void toUpperCase() {
String buffer = sb.toString();
buffer = buffer.toUpperCase();
sb.delete(0, sb.length());
sb.append(buffer);
}
private void cut() {
System.out.println("請輸入要剪切文字(開始索引號:結束索引號)");
String text = in.nextLine();
String[] texts = text.split(":");
int startIndex = Integer.parseInt(texts[0]);
int endIndex = Integer.parseInt(texts[1]);
clipBoard = sb.substring(startIndex, endIndex);
//刪除記事本文字
sb.delete(startIndex, endIndex);
}
private void paste() {
System.out.println("請輸入要粘貼文字(索引號)");
String text = in.nextLine();
int index = Integer.parseInt(text);
//粘貼
sb.insert(index, clipBoard);
}
private void copy() {
System.out.println("請輸入要複製文字 (開始索引號:結束索引號)");
String text = in.nextLine();
String[] texts = text.split(":");
int startIndex = Integer.parseInt(texts[0]);
int endIndex = Integer.parseInt(texts[1]);
clipBoard = sb.substring(startIndex, endIndex);
}
private void delete() {
System.out.println("請輸入要刪除文字(開始索引號:結束索引號)");
String text = in.nextLine();
String[] texts = text.split(":");
int startIndex = Integer.parseInt(texts[0]);
int endIndex = Integer.parseInt(texts[1]);
//刪除
sb.delete(startIndex, endIndex);
}
private void search() {
System.out.println("請輸入要查找文字(返回找到的文字的索引號)");
String text = in.nextLine();
int index = sb.indexOf(text);
if (index >= 0) {
System.out.println(text + " 索引號:" + index);
} else {
System.out.println(text + " 不存在");
}
}
private void replace() {
System.out.println("請輸入要替換文字 (要替換的文字:新文字)");
String text = in.nextLine();
String[] texts = text.split(":");
String oldText = texts[0];
String newText = texts[1];
//替換
String buffer = sb.toString();
buffer = buffer.replace(oldText, newText);
sb.delete(0, sb.length());//清空記事本文字
sb.append(buffer);
}
private void insert() {
System.out.println("請輸入要插入的文字: (索引號:要插入的文字)");
String text = in.nextLine();
String[] texts = text.split(":");
int index = Integer.parseInt(texts[0]);
String str = texts[1];
//插入文字
sb.insert(index, str);
}
private void append() {
System.out.println("請輸入要追加文字:");
String text = in.nextLine();
//追加文字
sb.append(text);
}
private void exit() {
flag = false;
System.exit(0);//退出應用程序
}
private void input() {
System.out.println("請輸入要編輯的文字:");
String text = in.nextLine();
//添加到 sb 記事本的緩衝區
sb.append(text);
}
public static void main(String[] args) {
new Test();
}
}