查找在Java中用双引号括起来的子字符串

问题描述 投票:0回答: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>'

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

示例:用户输入:ff0000因此,现在内容字符串必须将此行<g id="Group-36-Copy-4" fill="#FFFFFF">替换为<g id="Group-36-Copy-4" fill="#ff0000">

我被困住了。请帮助。

java string xml-parsing substring
2个回答
0
投票

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

content = content.replace(“#FFFFFF”,input)

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


0
投票

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

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));

此,如果要替换相同的变量,否则请使用其他变量。

© www.soinside.com 2019 - 2024. All rights reserved.