简体   繁体   English

如何从字符串中删除所有出现的 substring

[英]How to remove all occurrences of a substring from a string

I have a String(txt) and in this String is html code.我有一个字符串(txt),在这个字符串中是 html 代码。 I will search the String with txt.indexOf for ("< ac:structured-macro ac:macro-id=")我将使用 txt.indexOf 搜索字符串 ("< ac:structured-macro ac:macro-id=")

and delet it with StringBuffer(sb) sb.delete(Index, EndIndex).并使用 StringBuffer(sb) sb.delete(Index, EndIndex) 将其删除。 I will do this multiple times but when i do this with a while Loop it dosen't work and find only the index of the first element("ac:structured-macro ac:macro-id=").我将多次执行此操作,但是当我使用 while Loop 执行此操作时,它不起作用并且仅找到第一个元素的索引(“ac:structured-macro ac:macro-id=")。

Edit: The Main Problem, is that the id is always diffrent and i want to delet it too.编辑:主要问题是 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);

This is the output:这是 output:


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)

Thank You for your help!谢谢您的帮助!

Basically, the problem is to remove all occurrences of a certain html tag in the original string.基本上,问题是删除原始字符串中某个 html 标记的所有出现。

In java it may be accomplished with far less effort than your approach requires:在 java 中,它可以比您的方法所需的工作量少得多:

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

This code replaces all occurrences of a given string with an empty string, which is equivalent to removing them.此代码将所有出现的给定字符串替换为空字符串,这相当于删除它们。

Unfortunately, the code you posted isn't compiling.不幸的是,您发布的代码没有编译。 Thus, it doesn't make sense to analyze the reason it isn't working correctly.因此,分析它不能正常工作的原因是没有意义的。 I strongly suggest to review it before sending it to stackoverflow community.我强烈建议在将其发送到 stackoverflow 社区之前对其进行审查。

I think you would like to remove only the tag.我想你只想删除标签。 Because you have some line breaks因为你有一些换行符

String patternText = "(<)(/)?ac:structured-macro((?!>).)*>";
Pattern pattern = Pattern.compile(patternText, Pattern.DOTALL);
String result = pattern.matcher(txt).replaceAll("");
System.out.println(result);

Explanation for the regexp:正则表达式的解释:

  • The tags starts with (<) .标签以(<)开头。
  • You have a opening tag and a closing tag, so we need optional (/)?你有一个开始标签和一个结束标签,所以我们需要可选的(/)?
  • Next we have the literal ac:structured-macro接下来我们有文字ac:structured-macro
  • Than we search for everything execpt > over multiple lines with ((?.>).)*比我们用((?.>).)*在多行中搜索所有 execpt >
  • Last we need the closing > with >最后我们需要关闭>>

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

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