2016-10-08 187 views
0

好我有一個問題是我有這樣的輸入: -如何拆分Swift消息?

:21:ARI
:22:薩欽
:23:維魯
危險
:25:SEVI

我必須以這樣的方式拆分21個匹配ari,我分裂整個輸入通過使用分線器,但我不能匹配的價值危險與標籤23我的代碼是在下面,任何人有一個不同的邏輯,請幫助: -

public class HelloWorld{ 

public static void main(String []args){ 

StringBuilder sb=new StringBuilder(); 
    sb.append(":20:akshay"); 
    sb.append("\n"); 
    sb.append(":21:ari"); 
    sb.append("\n"); 
    sb.append(":22:sachin"); 
    sb.append("\n"); 
    sb.append(":23:viru"); 
    sb.append("\n"); 
    sb.append("hazard"); 
    sb.append("\n"); 
    sb.append(":25:sevi"); 
    String[] tok = sb.toString().split("\n"); 
    System.out.println(tok.length); 
    for(String string:tok){ 
     if(string.contains(":")){ 
      int first=string.indexOf(':'); 
      int last=string.lastIndexOf(':'); 
      String tag=string.substring(first+1,last); 
      String data=string.substring(last+1); 

     }else{ 

     } 

     System.out.println(sb); 


    } 





    } 
} 

回答

1

你的預期行爲是什麼? 23應該對應2個值 - 「病毒」和「危險」?

我可以建議標記+數據的每個片段都是通過組合字符「\ n:」分隔的。並且可以有多個由「\ n」分隔的數據元素用於每個標籤。

如果這是真的,那麼你可以修改你的splittng代碼如下,它應該工作

String tempString = sb.toString().substring(1); // Remove first colon 
    System.out.println("\nInitial string before splitting = " + tempString); 

    String[] tok = tempString.split("\n:"); // Split by new line and colon 
    System.out.println("\ntok.length = " + tok.length); 
    System.out.println("\nArray after splitting of initial string = " + Arrays.toString(tok)); 

    for(String string:tok){ 
     System.out.println("\nStarting to match values for string = " + string); 
     if(string.contains(":")){ 
      int last=string.lastIndexOf(':'); 
      String tag=string.substring(0,last); 
      String[] data=string.substring(last+1).split("\n"); 

      System.out.println("tag = " + tag + ", data = " + Arrays.toString(data)); 
     }else{ 
      System.out.println("String \"" + string + "\" does not contain ':' character."); 
     } 
    } 
0

如果你期待一個多行數據,分裂的「\ n」,並使用正則表達式

String[] tok = sb.toString().split("\n:"); 
    Pattern p = Pattern.compile(":?(.*):(.*)", Pattern.DOTALL); 
    for(String string:tok){ 
     Matcher matcher = p.matcher(string); 
     if (matcher.matches()) { 
      String tag = matcher.group(1); 
      String data = matcher.group(2); 
      System.out.println(tag+"_"+data); 
     } 
    } 
0

得到它傢伙感謝大家提出寶貴的建議

public class HelloWorld{ 

public static void main(String []args){ 

StringBuilder sb=new StringBuilder(); 
    sb.append(":20:akshay"); 
    sb.append("\n"); 
    sb.append(":21:ari"); 
    sb.append("\n"); 
    sb.append(":22:sachin"); 
    sb.append("\n"); 
    sb.append(":23:viru"); 
    sb.append("\n"); 
    sb.append("hazard"); 
    sb.append("\n"); 
    sb.append(":25:sevi"); 
    String sb1=sb.substring(sb.toString().indexOf(':')+1); 

    String[] tok = sb1.toString().split("\n:"); 
    System.out.println(tok.length); 
    for(String string:tok){ 
     // if(string.contains(":")){ 
      String[] data=string.split(":"); 

      String tag=data[0]; 
      String msgData=data[1]; 
      System.out.println("tag::"+tag+"::data::"+msgData); 

     // } 


    } 





    } 
}