简体   繁体   English

Java-从jeditorpane中的文本中删除特定字符

[英]Java - removing specific characters from text in jeditorpane

I would appreciate some help modifying a function! 我希望能对修改功能有所帮助!

The aims: Make an array list of specific characters. 目的:列出特定字符的数组。 Write a method to remove the characters specified in the arraylist from text JEditorpane. 编写一种方法,以从文本JEditorpane中删除arraylist中指定的字符。

So Far: Made an arraylist of characters, Wrote a function to remove the characters. 到目前为止:创建了一个字符数组列表,编写了一个删除字符的函数。 made a gui that includes jeditorpane 制作了一个包含jeditorpane的gui

The issue: The function works, and remove characters that I println to console via a string. 问题:该函数有效,并删除了我通过字符串println控制台的字符。

I am struggling to make the function remove the characters from the text document that i open into JEditorpane. 我正在努力使函数从我打开到JEditorpane的文本文档中删除字符。

The code in short: 简短的代码:

     private static ArrayList<Character> special = new  ArrayList<Character>(Arrays.asList('a','b','h'));



    public class removing implements ActionListener {
    public void actionPerformed(ActionEvent e) {

documentpane is the name of my jeditorpane documentpane是我的jeditorpane的名称

if I change, document.chatAt, to test.chatAt, (which is printing to console, this works. 如果我将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);

Thanks for any help in advance. 感谢您的任何帮助。

As Document doesn't have charAt method you first need to extract the content of the document. 由于Document没有charAt方法,因此您首先需要提取文档的内容。 This can be done by the following: 这可以通过以下方式完成:

String content = document.getText(0, document.getLength());

Then when using your for-loop with content it should work. 然后,在将for-loop用于content它应该可以工作。 So your code would look like this: 因此,您的代码如下所示:

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);

How about this : 这个怎么样 :

    String outputText =document.getText(0,docuemnt.getLength()).replaceAll("[a|b|c]"," ");
   //set regex that you want 
    System.out.println(outputText);

You can use document.getText(0,docuemnt.getLength()) As lino suggested. 您可以按照lino的建议使用document.getText(0,docuemnt.getLength())。 But I prefer regex cause you do not have to loop and check, Using StringBuilder instead of concatenation is a better practice 但是我更喜欢使用正则表达式,因为您不必循环检查,使用StringBuilder而不是连接是更好的做法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM