从字符串中删除字符
源代码在线查看: g.java
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class G extends Applet implements ActionListener
{
Label a,b;
TextField input1,input2;
String c,f;
int i=0,j=0,d;
public void init()
{
a=new Label("请输入一字符串:");
b=new Label("请输入一字符:");
input1=new TextField(20);input2=new TextField(1);
add(a);
add(input1);
add(b);
add(input2);
input2.addActionListener(this);
}
public void paint(Graphics g)
{
g.drawString("经删除后的字符串为:"+f,10,70);
}
public void actionPerformed(ActionEvent e)
{
f="";
c=input1.getText();
d=(int)(input2.getText().charAt(0));
/*do
{
i=c.indexOf(d,0);
c.deleteCharAt(i);
}while(i!=-1);*/
while((j=c.indexOf(d,i))!=-1)
{
System.out.println(i+","+j+","+(j-i));
f=f+c.substring(i,j); //取子字符串
i=j+1;
}
f=f+c.substring(i,c.length());
repaint();
i=0;
}
}