Java-从窗格中的文本中删除特定字符
我将感谢一些帮助修改一个函数 目标是: 制作特定字符的数组列表。 编写一个方法,从文本窗格中删除arraylist中指定的字符 到目前为止: 制作了一个角色列表, 编写了一个函数来删除字符。 制作了一个包含jeditorpane的gui 问题是: 该函数可以工作,并删除我通过字符串打印到控制台的字符 我正在努力使函数从我打开到JEditorpane的文本文档中删除字符 简而言之:Java-从窗格中的文本中删除特定字符,java,arrays,string,jeditorpane,Java,Arrays,String,Jeditorpane,我将感谢一些帮助修改一个函数 目标是: 制作特定字符的数组列表。 编写一个方法,从文本窗格中删除arraylist中指定的字符 到目前为止: 制作了一个角色列表, 编写了一个函数来删除字符。 制作了一个包含jeditorpane的gui 问题是: 该函数可以工作,并删除我通过字符串打印到控制台的字符 我正在努力使函数从我打开到JEditorpane的文本文档中删除字符 简而言之: private static ArrayList<Character> special = n
private static ArrayList<Character> special = new ArrayList<Character>(Arrays.asList('a','b','h'));
public class removing implements ActionListener {
public void actionPerformed(ActionEvent e) {
private static ArrayList special=new ArrayList(Arrays.asList('a','b','h'));
公共类删除实现ActionListener{
已执行的公共无效操作(操作事件e){
documentpane是我的jeditorpane的名称
如果我将document.chatAt更改为test.chatAt(它正在打印到控制台),这会起作用
Document document = documentpane.getDocument();
String test = "hello world?";
String outputText = "";
for (int i = 0; i < document.getLength(); i++) {
Character c = new Character(document.charAt(i));
if (!special.contains(c))
outputText += c;
else
outputText += " ";
}
System.out.println(outputText);
Document Document=documentpane.getDocument();
字符串测试=“你好,世界?”;
字符串outputText=“”;
对于(int i=0;i
提前感谢您的帮助。这个怎么样:
String outputText =document.getText(0,docuemnt.getLength()).replaceAll("[a|b|c]"," ");
//set regex that you want
System.out.println(outputText);
您可以使用document.getText(0,docuemnt.getLength())正如lino所建议的。但我更喜欢正则表达式,因为您不必循环和检查,使用StringBuilder而不是串联是更好的做法,因为
文档没有charAt
方法,您首先需要提取文档的内容。这可以通过以下方法完成:
String content = document.getText(0, document.getLength());
然后,当将for循环
与内容
一起使用时,它应该可以工作。因此,您的代码如下所示:
Document document = documentpane.getDocument();
String content = document.getText(0, document.getLength());
String outputText = "";
for (int i = 0; i < content.length(); i++) {
Character c = new Character(content.charAt(i));
if (!special.contains(c))
outputText += c;
else
outputText += " ";
}
System.out.println(outputText);
Document Document=documentpane.getDocument();
字符串内容=document.getText(0,document.getLength());
字符串outputText=“”;
对于(int i=0;i
文档
没有字符
方法Hi Lino,感谢您的回复,我已经实现了您的解决方案,只有一个错误。对于(int I=0;Ilength()
这就是我的意思。EA,我想出来了,周围都是try-catch,再次感谢您的更新。最后一件事是删除字符,并将JEditorpane中的内容粘贴到控制台,删除字符。(几乎完美),我如何修改它以更新JEditorpane中的文本?谢谢!!