我如何将这个代码整理成java中的循环?

时间:2013-05-17 15:32:32

标签: java string tags extract

 public class TagHandler {

private final String START = "<START ";
private final String END = "<END ";


    public String handleTag(String buf, String[] attrList) {  

     String startPattern1 = START+attrList[0]+">";
    String endPattern1 = END+attrList[0]+">";

    String startPattern2 = START+attrList[1]+">";
    String endPattern2 = END+attrList[1]+">";

    String startPattern3 = START+attrList[2]+">";
    String endPattern3 = END+attrList[2]+">";

    String startPattern4 = START+attrList[3]+">";
    String endPattern4 = END+attrList[3]+">";

    String startPattern5 = START+attrList[4]+">";
    String endPattern5 = END+attrList[4]+">";


           String extract1 = new String(buf);
    String extract2 = new String(buf);
    String extract3 = new String(buf);
    String extract4 = new String(buf);
    String extract5 = new String(buf);

            extract1 = extract1.substring(extract1.indexOf(startPattern1)+startPattern1.length(), extract1.indexOf(endPattern1));
    extract2 = extract2.substring(extract2.indexOf(startPattern2)+startPattern2.length(), extract2.indexOf(endPattern2));
    extract3 = extract3.substring(extract3.indexOf(startPattern3)+startPattern3.length(), extract3.indexOf(endPattern3));
    extract4 = extract4.substring(extract4.indexOf(startPattern4)+startPattern4.length(), extract4.indexOf(endPattern4));
    extract5 = extract5.substring(extract5.indexOf(startPattern5)+startPattern5.length(), extract5.indexOf(endPattern5));

 String s = ("BLOPABP"+extract1) + ("\nBLOPCALL"+extract2) +("\nBLOPEXP"+extract3) +("\nBLOPHEAD"+extract4)+("\nBLOPMAJ"+extract5);

return s;
  }

我如何将上面的代码整理成某种循环?基本上我有一个文件,我正在阅读并提取标签内的数据,我将标签传递给这个TagHandler方法,并将提取的数据作为字符串返回,标签标题没有“&lt; START&gt;”和“&lt; END TAG”&gt;只在开始标记上留下标题。

3 个答案:

答案 0 :(得分:1)

你走了。这应该做你想要的。

public class TagHandler {

private final String START = "<START ";
private final String END = "<END ";

public String handleTag(String buf, String[] attrList) {

    String[] blop = {"BLOPABP", "BLOPCALL", "BLOPEXP", "BLOPHEAD", "BLOPMAJ"};
    String s = "";

    for (int i = 0; i < attrList.length; i++) {
        String startPattern = START+attrList[i]+">";
        String endPattern = END+attrList[i]+">";
        String extract = buf.substring(buf.indexOf(startPattern)+startPattern.length(), buf.indexOf(endPattern));
        s += blop[i]+extract;
        if (i < attrList.length-1) {
            s +=  "\n";
        }
    }
    return s;

}

}

如果attrList有超过5个元素,请留意超出范围的异常。

答案 1 :(得分:0)

您可以尝试这样的方法,如果可以的话,优化它:

public String handleTag(String buf, String[] attrList) {
    StringBuilder temp = new StringBuilder();
    final String[] prefix = {"BLOPABP","\nBLOPCALL","\nBLOPEXP",
                       "\nBLOPHEAD","\nBLOPMAJ"};
    for(int i=0;i<attrList.length;i++){
        String startPattern = START+attrList[i]+">";
        String endPattern = END+attrList[i]+">";
        String extract = new String(buf);
        extract = extract.substring(
                extract.indexOf(startPattern)+startPattern.length(), 
                extract.indexOf(endPattern));
        temp.append(prefix[i%5]+extract);
    }

  return temp.toString();
}

答案 2 :(得分:0)

这应该有效。如果您使用的是Java 7,则可以将= new ArrayList<String>替换为= new ArrayList<>()

private final String START = "<START ";
private final String END = "<END ";
List<String> startPatterns = new ArrayList<String>();//can use ArrayList<> instead if java 1.7
List<String> stringExtracts = new ArrayList<String>();
final String[] tags = new String[]{"BLOPABP","\nBLOPCALL","\nBLOPEXP","\nBLOPHEAD","\nBLOPMAJ"};

public String handleTag(String buf, String[] attrList) {
    int numPatterns = tags.length;
    String s;
    String extract = new String(buf);
    for(int i=0; i<numPatterns; i++){
        String startPattern = START+attrList[i]+">";
        startPatterns.add(startPattern);
        String endPattern = END+attrList[i]+">";
        endPatterns.add(endPattern);
        String extract = extract.substring(extract.indexOf(startPattern)+startPattern.length(), extract.indexOf(endPattern));
        stringExtracts.add(extract);
        s += tags[i] + extract;
    }
    return s;
}

这假设您需要再次访问各个startPatterns,endPatterns和stringExtracts,而不仅仅是s。如果您只需要s然后丢弃ArrayLists - 它将如下工作:

private final String START = "<START ";
private final String END = "<END ";
final String[] tags = new String[]{"BLOPABP","\nBLOPCALL","\nBLOPEXP","\nBLOPHEAD","\nBLOPMAJ"};

public String handleTag(String buf, String[] attrList) {
    int numPatterns = tags.length;
    String s;
    String extract = new String(buf);
    for(int i=0; i<numPatterns; i++){
        String startPattern = START+attrList[i]+">";
        String endPattern = END+attrList[i]+">";
        String extract = extract.substring(extract.indexOf(startPattern)+startPattern.length(), extract.indexOf(endPattern));
        s += tags[i] + extract;
    }
    return s;
}