У меня есть строка (txt) и в этой строке HTML-код. Я буду искать строку с помощью txt.indexOf ("
и удалю ее с помощью StringBuffer (sb) sb.delete (Index, EndIndex). Я буду делать это несколько раз, но когда я делаю это с циклом while, он не работает и находит только индекс первого элемента ("ac: Structured-macro AC: macro-id =").
Редактировать: Главная проблема в том, что идентификатор всегда различен, и я тоже хочу удалить его.
String txt = textArea1.getText();
/*somthing like this <p>
<p>
<br/>
</p>
<ac:structured-macro ac:macro-id="74563a55-dc09-41a1-acaa-7c6338ab4014" ac:name="unmigrated-wiki-markup"
ac:schema-version="1">
<ac:plain-text-body>
<![CDATA[
Lorem ipsum dolor sit amet, consetetur sadipscing elitr
]]>
</ac:plain-text-body>
</ac:structured-macro>
<p>
<br/>
</p>
<ac:structured-macro ac:macro-id="bc7e6c08-82c8-4ee9-8582-b773914857f" ac:name="unmigrated-wiki-markup"
ac:schema-version="1">
<ac:plain-text-body>
<![CDATA[
Lorem ipsum dolor sit amet, consetetur sadipscing elitr
]]>
</ac:plain-text-body>
</ac:structured-macro>
*/
int StartIndexOfMacroID = 0;
int indexEndOfMacroID = 0;
StringBuffer sb = new StringBuffer(txt);
while (StartIndexOfMacroID != -1) {
StartIndexOfMacroID = txt.indexOf("<ac:structured-macro ac:macro-id=");
indexEndOfMacroID = StartIndexOfMacroID + 159;
sb.delete(StartIndexOfMacroID, indexEndOfMacroID);
System.out.println(StartIndexOfMacroID);
);
txt = sb.toString();
System.out.println(StartIndexOfMacroID);
System.out.println(indexEndOfMacroID);
textArea2.setText(txt);
Это вывод:
17
176
159
318
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: start -1, end 158, length 282
at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1724)
at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:863)
at java.base/java.lang.StringBuffer.delete(StringBuffer.java:474)
at com.company.Main$1.actionPerformed(Main.java:115)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:270)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6651)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6416)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5026)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4858)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:778)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:751)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:749)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:748)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Спасибо за вашу помощь!