簡體   English   中英

如何檢查是否有新行?

[英]How can I check if there is a new line?

這個程序應該轉換為:

\hyp76{a,1+a-b}b{xy}

至:

\HyperpFq{7}{6}@@{a,1+a-b}{b}{xy}.

如您所見,如果組的內容是一個字符,則沒有括號。 但是,當其中一個帶方括號的組位於一行的末尾時,它將跳過該組並將其視為單個字符(不帶方括號)的大小寫。 如何避免這種情況? 謝謝。 這是我的代碼:

static int checkNestedBrackFront(String line, int pos){
    int count=0;
    for(int i=pos;i<line.length();i++ ){
        if(line.charAt(i)=='{')
            count++;
        if(line.charAt(i)=='}'&&count==0)
            return i;
        if(line.charAt(i)=='{')
            count--;
    }
    return 0;
}

line = new Scanner(new File("KLSadd.tex")).useDelimiter("\\Z").next();
    PrintWriter writer = new PrintWriter("Converted.tex");
    while(line.contains("\\hyp76")){
            int posHyp = line.indexOf("\\hyp76");
            String beforeHyp = line.substring(0,posHyp);
            int start = posHyp+7;   
            String firstArgs = line.substring(start, line.indexOf("}", start));
            if(line.charAt((line.indexOf("}", start)+1))!='{'&&!(line.substring(start, start+4).contains("\n"))){ //this is to check for single characters
                secArgs = line.substring(line.indexOf("}", start) + 1,line.indexOf("}", start) + 2 );
                posSec=line.indexOf("}", start)+1;
            }
            else {
                int posBrack = line.indexOf("{", line.indexOf("}", start));
                posSec = checkNestedBrackFront(line, posBrack+1);
                secArgs = line.substring(posBrack+1, posSec);
            }
            if((line.charAt(posSec+1)!='{')&&!(line.substring(posSec,posSec+4).contains("\n"))){ //this is to check for single characters
                System.out.println(line.charAt(posSec+1)+"hello");
                 thirdArgs= line.substring(posSec+1,posSec+2);
                 afterHyp=line.substring(posSec+2);
            }
            else{
            int posThirdBrack = line.indexOf("{", posSec);
            int posThird = checkNestedBrackFront(line, posThirdBrack+1);
            thirdArgs = line.substring(posThirdBrack+1,posThird);
            afterHyp = line.substring(posThird+1);
            }
            line=beforeHyp+"\\HyperpFq{7}{6}@@{"+firstArgs+"}{"+secArgs+"}{"+thirdArgs+"}"+afterHyp;
        }
      writer.print(line);
    writer.close();

有點令人困惑,但是據我了解,您希望檢測到一條新線:

 PrintWriter writer = new PrintWriter("Converted.tex");
        Scanner scanner = new Scanner(new File("KLSadd.txt"))
                .useDelimiter("\n");
        while (scanner.hasNext()) {
        String  lne = scanner.next();
        String[] lines =  lne.split("YOUR DELIMETER");
        for(int j=0;j<lines.length;j++){
            line = lines[j];
            //the rest of your logic  goes here
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM