如何从每一行拆分最后两个字符串以重复替换它们

时间:2014-03-17 08:53:29

标签: java split

我正在尝试从存储坐标值的数据库写入文件。 我要保存的文件是SVG文件,代码我对如何替换moveTo坐标和curveTo坐标感到困惑?另外如何在每一行重复C命令?数据以这种形式存储在数据库中作为字符串

355.22 632.0771 355.574 631.338 315.228 632.077 455.22 632.0771 355.574 631.338 335.228 632.077 555.22 632.0771 355.574 631.338 375.228 632.077 655.22 232.0771 355.574 631.338 385.228 632.077 755.22 332.0771 355.574 631.338 365.228 632.077 255.22 432.0771 355.574 631.338 395.228 632.077 155.22 532.0771 355.574 631.338 355.228 632.077

最后2个值是移动变量,即:move="355.228 632.077";

并且每一行被视为一条曲线,曲线变量即:curve="355.22 632.0771 355.574 631.338 315.228 632.077";在我的例子中有7条曲线。 例如:

public class WriteToSVG {

private Formatter f;

public void openFile(){
    try{
        //To Open & create the a File
        f=new Formatter("C:\\demo.svg");
    }
    catch(Exception e){
        System.out.println("Error occured");
    }
}

public void addRecords(){
    try{
        //Add Data to the File
        String str="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
                "<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363)  -->\n" +
                "\n" +
                .....";
        f.format(str);
        String space=" ";
        String fill="003399";
        String fill_opacity="1";
        String strock="none";
        String move = "";
        String[] moveSplit = move.split("\\rs");
        String curve="1022.9594,426.89007 1022.7136,426.30999 1022.5051,425.84922";
        String id="path4044-3";
        String nodeType="cccccccccccccccccc"; 
        f.format("<path%s" +"style="+"\"fill:#%s;fill-opacity:%s;stroke:%s\""+"\nd=",space,fill,fill_opacity,strock);
        f.format("\"M%s",move);//moveTo variable
        f.format( "C%s",curve);//curveTo variable
        f.format("z\""+"\nid=\"%s\""+"\ninkscape:connector-curvature=\"0\""+ "\n sodipodi:nodetypes=\"%s\""+"/>",space,fill,fill_opacity,strock,id,nodeType);
        f.format("</svg>");
    System.out.println("Data added to the File successfully");
      }
    catch(Exception e){
        System.out.println("Error occured");
    }
}
public void closeFile(){
    f.close();
}
public static void main(String[] args) {
    WriteToSVG w=new WriteToSVG();
    w.openFile();
    w.addRecords();
    w.closeFile();
}}

如何根据以上几点使用拆分,我想从每一行使用最后2个点?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

final String[] lines = {
    "355.22 632.0771 355.574 631.338 315.228 632.077",
    "455.22 632.0771 355.574 631.338 335.228 632.077",
    "555.22 632.0771 355.574 631.338 375.228 632.077",
    "655.22 232.0771 355.574 631.338 385.228 632.077",
    "755.22 332.0771 355.574 631.338 365.228 632.077",
    "255.22 432.0771 355.574 631.338 395.228 632.077",
    "155.22 532.0771 355.574 631.338 355.228 632.077"
};

for(final String line : lines) {
    final String[] parts = line.split(" (?=[\\d.]* [\\d.]*$)");
    final String[] curves = parts[0].split(" ");
    final String[] moves = parts[1].split(" ");

    // here, curves contains your first four numbers and moves contains the two last ones
}

工作原理:

  • 对于每一行,我们将内容分为两部分。
  • 对于第一部分,在空格上分割其内容会给出曲线坐标列表。
  • 对于第二部分,在空格上拆分其内容会显示移动坐标列表。

例如,考虑行"355.22 632.0771 355.574 631.338 315.228 632.077"

line = "355.22 632.0771 355.574 631.338 315.228 632.077";
// splitting here                      ^
parts = {"355.22 632.0771 355.574 631.338", "315.228 632.077"};

parts[0] = "355.22 632.0771 355.574 631.338";
// splitting      ^        ^       ^
curves = {"355.22", "632.0771", "355.574", "631.338"};

parts[1] = "315.228 632.077";
// splitting       ^
moves = {"315.228", "632.077"};

我希望这会有所帮助;)