gpt4 book ai didi

java - 如何从字符串中删除所有出现的子字符串

转载 作者:行者123 更新时间:2023-12-02 09:25:08 32 4
gpt4 key购买 nike

我有一个字符串(txt),这个字符串是html代码。我将使用 txt.indexOf 搜索字符串(“< ac:结构化宏 ac:宏 id=")

并使用 StringBuffer(sb) sb.delete(Index, EndIndex) 删除它。我会多次执行此操作,但是当我使用 while 循环执行此操作时,它不起作用并且仅找到第一个元素的索引(“ac:structed-macro ac:macro-id =”)。

编辑:主要问题是 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)

感谢您的帮助!

最佳答案

基本上,问题是删除原始字符串中出现的所有特定 html 标记。

在 java 中,它可能比您的方法所需的工作量少得多:

final String htmlContent = textArea1.getText();
final String filteredHtmlContent = htmlContent.replaceAll("<ac:structured-macro.*?</ac:structured-macro>", "");

此代码将所有出现的给定字符串替换为空字符串,这相当于删除它们。

不幸的是,您发布的代码无法编译。因此,分析它不能正常工作的原因是没有意义的。我强烈建议在将其发送到 stackoverflow 社区之前对其进行审阅。

关于java - 如何从字符串中删除所有出现的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58393900/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com