有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在当前打开的编辑器中用另一个字符串替换特定字符串?

嗨,我正在做一个eclipse插件项目来创建一个IDE。在我的IDE中 检查当前打开的编辑器是否有特定字符串,并应 替换为在侧面的文本框中输入的字符串 看法我可以访问编辑器,但如果我搜索特定的 字符串,并用用户输入的输入替换该字符串 这不管用

 IDocumentProvider provider=((AbstractTextEditor) ieditorpart).getDocumentProvid();
 IDocument doc = provider.getDocument(ieditorpart.getEditorInput());  
 String content = doc.get();
 pos=content.compareTo("\\/\\*ProbeEnd\\*\\/");
 doc.replace(pos,5, "hello");

但这不管用。。。在这里,我刚刚给出了替代品 字符串为hello,但该值应取自文本框

访问编辑器有错误吗?我应该用这个吗 有什么方法可以做到这一点,或者有什么方法可以实现这一点?可以 有人帮我吗


共 (2) 个答案

  1. # 1 楼答案

    从编辑器中调用firePropertyChange(IEditorPart.PROP_INPUT)

  2. # 2 楼答案

    为什么变量“pos”是compareTo值(-1,0,1)?compareTo返回两个字符串的字典顺序

    IDocument的替换方法有三个参数:

    • int offset-文档中的偏移量,其中应插入“text”
    • int length——从“offset”开始的长度,应该被覆盖。长度0表示插入
    • 字符串文本-替换文本

    例如:

    String oldContent = doc.get();
    assert oldContent.equals("TestingText");
    
    String replaceText = "REPLACE";
    
    doc.replace(5,3,replaceText);
    
    String newContent = doc.get();
    assert newContent.equals("TestiREPLACEext");
    //offset 5 is the position after 'Testi'
    //length 3 means 'ngT' (starting from the offset) should be replaced
    //REPLACE is the newText