【问题标题】:Finding a substring that is enclosed withing double quotes in java在java中查找用双引号括起来的子字符串
【发布时间】:2020-04-29 10:16:50
【问题描述】:

我的代码:

  String content = '<?xml version="1.0" encoding="UTF-8"?>
        <svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" 
        xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
        <!-- Generator: Sketch 64 (93537) - https://sketch.com -->
        <title>Group 36 Copy 4</title>
        <desc>Created with Sketch.</desc>
        <g id="Portal" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
            <g id="Group-36-Copy-4" fill="#FFFFFF">
                <path d="M0,7.162 L6,8.877 L6.00036056,9 C6.00036056,10.0543618 6.81623835,10.9181651 7.85109822,10.9945143 L8.00036056,11 C9.10493006,11 10.0003606,10.1045695 10.0003606,9 L10.0003606,9 L10.0003606,8.98076197 L16,7.266 L16.0003606,14 C16.0003606,15.1045695 15.1049301,16 14.0003606,16 L2.00036056,16 C0.895791064,16 0.000360563943,15.1045695 0.000360563943,14 L0,7.162 Z M14.0003606,4 C15.1049301,4 16.0003606,4.8954305 16.0003606,6 L16,6.226 L9.99950581,7.94095939 C9.96827529,6.8636983 9.0851817,6 8.00036056,6 C6.94978154,6 6.08839609,6.81003398 6.00669844,7.83960773 L0,6.122 L0.000360563943,6 C0.000360563943,4.8954305 0.895791064,4 2.00036056,4 L14.0003606,4 Z" id="Combined-Shape"></path>
                <rect id="Rectangle" x="7.00036056" y="7" width="2" height="3" rx="1"></rect>
                <path d="M10.0003606,0 C11.0547224,0 11.9185257,0.815877791 11.9948748,1.85073766 L12.0003606,2 L12.0003606,4 L10.0003606,4 L10.0003606,2 L6.00036056,2 L6.00036056,4 L4.00036056,4 L4.00036056,2 C4.00036056,0.945638205 4.81623835,0.0818348781 5.85109822,0.00548573643 L6.00036056,0 L10.0003606,0 Z" id="Path-21" fill-rule="nonzero"></path>
            </g>
        </g>
    </svg>'

我在 java 中有一个名为“content”的字符串,其中包含用户上传的任何 svg 文件的内容。现在我的任务是用我的用户给出的输入替换这个属性 fill="#FFFFFF" 中的值。

示例:用户输入:ff0000 所以现在内容字符串必须将这一行 &lt;g id="Group-36-Copy-4" fill="#FFFFFF"&gt; 替换为 &lt;g id="Group-36-Copy-4" fill="#ff0000"&gt;

我被困住了。请帮忙。

【问题讨论】:

标签: java string xml-parsing substring


【解决方案1】:

如果您在内容中提到的填充永远不会改变,您可以使用替换功能。

content = content.replace("#FFFFFF",input)

参考:https://www.javatpoint.com/java-string-replace

【讨论】:

  • 是的。填充值变化。我只是为了解释这个问题。但事实是我不知道填充的值是多少。这取决于用户上传的 svg 文件类型。
  • 你可以解析那个字符串
  • 我可以通过使用 DOM 解析器来实现吗?因为在我的项目中,我在各个地方都看到过使用 DOM。
【解决方案2】:

首先,您的代码不可编译,因为您使用的是字符类型的单引号,您需要为您的字符串使用双引号并转义所有内部双引号:

String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><svg width=\"16px\" height=\"16px\" viewBox=\"0 0 16 16\" version=\"1.1\"xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><!-- Generator: Sketch 64 (93537) - https://sketch.com --><title>Group 36 Copy 4</title><desc>Created with Sketch.</desc><g id=\"Portal\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"><g id=\"Group-36-Copy-4\" fill=\"#FFFFFF\"><path d=\"M0,7.162 L6,8.877 L6.00036056,9 C6.00036056,10.0543618 6.81623835,10.9181651 7.85109822,10.9945143 L8.00036056,11 C9.10493006,11 10.0003606,10.1045695 10.0003606,9 L10.0003606,9 L10.0003606,8.98076197 L16,7.266 L16.0003606,14 C16.0003606,15.1045695 15.1049301,16 14.0003606,16 L2.00036056,16 C0.895791064,16 0.000360563943,15.1045695 0.000360563943,14 L0,7.162 Z M14.0003606,4 C15.1049301,4 16.0003606,4.8954305 16.0003606,6 L16,6.226 L9.99950581,7.94095939 C9.96827529,6.8636983 9.0851817,6 8.00036056,6 C6.94978154,6 6.08839609,6.81003398 6.00669844,7.83960773 L0,6.122 L0.000360563943,6 C0.000360563943,4.8954305 0.895791064,4 2.00036056,4 L14.0003606,4 Z\" id=\"Combined-Shape\"></path><rect id=\"Rectangle\" x=\"7.00036056\" y=\"7\" width=\"2\" height=\"3\" rx=\"1\"></rect><path d=\"M10.0003606,0 C11.0547224,0 11.9185257,0.815877791 11.9948748,1.85073766 L12.0003606,2 L12.0003606,4 L10.0003606,4 L10.0003606,2 L6.00036056,2 L6.00036056,4 L4.00036056,4 L4.00036056,2 C4.00036056,0.945638205 4.81623835,0.0818348781 5.85109822,0.00548573643 L6.00036056,0 L10.0003606,0 Z\" id=\"Path-21\" fill-rule=\"nonzero\"></path></g></g></svg>";

那么你可以使用String的替换:

content = content.replace("fill=\"#FFFFFF\"", String.format("fill=\"%s\"", replacement));

这个,如果你想在同一个变量中替换,否则使用不同的。

【讨论】:

  • 用户正在上传 svg 文件,并且每次内容都会更改,但我所知道的是第二个 标签的填充属性将保留,但它的值仍然未知。这就是为什么我问我想提取双引号之间的值,不管它是什么。这样我就可以用用户提供的值替换它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2019-05-11
  • 1970-01-01
相关资源
最近更新 更多