import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class prog5 extends Applet implements ActionListener
{
String originalString, modifiedString;
int delChar;
Label orgStringLbl = new Label("原字符串:");
Label delCharLbl = new Label("需去除字符:");
TextField orgStringTfd = new TextField(20);
TextField delCharTfd = new TextField(1);
Button modifyBtn = new Button("删除字符");
public void init()
{
add(orgStringLbl);
add(orgStringTfd);
add(delCharLbl);
add(delCharTfd);
add(modifyBtn);
orgStringTfd.setText("");
delCharTfd.setText("");
originalString = "";
modifiedString = "";
modifyBtn.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawString(modifiedString, 10, 150);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == modifyBtn)
{
originalString = orgStringTfd.getText();
delChar = (int)(delCharTfd.getText().charAt(0));
modifiedString = "";
int i=0, j=0;
while( (j=originalString.indexOf(delChar,i)) != -1 )
{
System.out.println(i + "," + j + "," + (j-i));
System.out.println(originalString);
modifiedString = modifiedString
+ originalString.substring(i,j);
System.out.println(modifiedString);
i = j+1;
}
modifiedString = modifiedString
+ originalString.substring(i,originalString.length());
repaint();
}
else
{
showStatus("事件" + ae.toString() + "未定义处理操作。");
orgStringTfd.setText("");
delCharTfd.setText("");
originalString = "";
modifiedString = "";
}
}
}
public static boolean isReversable(String s)
{
for(int i=0,j=s.length()-1; i<j; i++,j--)
{
if(s.charAt(i) != s.charAt(j))
return false;
}
return true;
}
}以前未懂今天弄懂的,呵呵小高兴一下!
该贴已经同步到 杨哥的微博 |