2017-10-12 94 views
0

我想解析輸入,例如:GH123FG12B1A58。使用標記中的鍵找到多個字符串位置

'GH'/'FG'/'A'/'B'將以相同順序存在於所有標籤中,但位置不同。例如:GH14555523FG1555552B55551A55558

需要找到每一個鍵

我看這可以通過使用行話完成後的值,獲得開始&結束索引?有沒有其他辦法來實現這一點?

回答

0
import java.util.Scanner; 


public class Shipparse { 

    public static void main(String[] args) { 

      @SuppressWarnings("resource") 
      Scanner Iname = new Scanner(System.in); 
      System.out.println("Enter the invoice :"); 
      String Maintag = Iname.nextLine(); 

      String GH = "GH"; 
      String FG = "FG"; 
      String AB = "AB"; 
      String B = "B"; 
      String A = "A"; 

      int cus = Maintag.indexOf(GH); 
      int cys = Maintag.indexOf(FG); 
      int ats = Maintag.indexOf(AB); 
      int ss = Maintag.indexOf(B); 
      int se = Maintag.indexOf(A); 

      int tlength = Maintag.length(); 

      StringBuilder str = new StringBuilder(Maintag); 

      String cnum; 

      if (ats == -1) { 

       cnum = str.substring((cus +2) , cys); 
       System.out.println("Customer :" + cnum); 

       String cyn = str.substring((cys + 2), ss); 
       System.out.println("Agent :" + cyn); 

      } else { 

       cnum = str.substring((cus +2) , ats); 
       System.out.println("Customer :" + cnum); 

       String cyn = str.substring((ats + 2), ss); 
       System.out.println("Company:" + cyn); 
      } 

      String spoint = str.substring((ss +1) , se); 

      System.out.println("TYPE NUM:" + spoint); 

      String send = str.substring((se +1) , tlength); 
      System.out.println("FIELD NUM :" + send); 

    } 
} 
相關問題